********************************************************************************
**  TITLE:      ca2019_ces_web						                          **  
**  AUTHORS:    P. Mongrain, N. Fréchet, B. Thompson Collart, and Y. Dufresne **
**  DATA:       2019 Canadian Election Study - Online Survey v1.0		      **
**	DATE:		February 2025	 					                          **	
**  VERSION:	Stata 17					                                  **	
********************************************************************************

* Note: The data file needs to be downloaded from its original source (see the "surveys.text" file)

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "ca2019_ces_web", replace

* Open the dataset

use "2019 Canadian Election Study - Online Survey v1.0 (With Political knowledge coded).dta", clear

* Election

gen election = "ca2019"

* Type of survey

gen type = "web"

* Type of expectation item

gen probability = 1

* Respondent ID

encode cps19_ResponseId, gen(responseid)

* Survey weight

merge 1:1 election responseid using ca2019_web_weight
drop _merge


***********************
** DISTRICT OUTCOMES **
***********************

* Note: 1 = Conservative, 2 = Liberal, 3 = NDP, 4 = BQ, 5 = Greens, 6 = People's Party, 88 = other

gen district_outcome = .

replace district_outcome = 2 if constituencynumber == 10001
replace district_outcome = 2 if constituencynumber == 10002
replace district_outcome = 2 if constituencynumber == 10003
replace district_outcome = 2 if constituencynumber == 10004
replace district_outcome = 2 if constituencynumber == 10005
replace district_outcome = 3 if constituencynumber == 10006
replace district_outcome = 2 if constituencynumber == 10007
replace district_outcome = 2 if constituencynumber == 11001
replace district_outcome = 2 if constituencynumber == 11002
replace district_outcome = 2 if constituencynumber == 11003
replace district_outcome = 2 if constituencynumber == 11004
replace district_outcome = 2 if constituencynumber == 12001
replace district_outcome = 2 if constituencynumber == 12002
replace district_outcome = 2 if constituencynumber == 12003
replace district_outcome = 2 if constituencynumber == 12004
replace district_outcome = 2 if constituencynumber == 12005
replace district_outcome = 2 if constituencynumber == 12006
replace district_outcome = 2 if constituencynumber == 12007
replace district_outcome = 2 if constituencynumber == 12008
replace district_outcome = 2 if constituencynumber == 12009
replace district_outcome = 2 if constituencynumber == 12010
replace district_outcome = 1 if constituencynumber == 12011
replace district_outcome = 2 if constituencynumber == 13001
replace district_outcome = 2 if constituencynumber == 13002
replace district_outcome = 5 if constituencynumber == 13003
replace district_outcome = 1 if constituencynumber == 13004
replace district_outcome = 2 if constituencynumber == 13005
replace district_outcome = 2 if constituencynumber == 13006
replace district_outcome = 2 if constituencynumber == 13007
replace district_outcome = 1 if constituencynumber == 13008
replace district_outcome = 2 if constituencynumber == 13009
replace district_outcome = 1 if constituencynumber == 13010
replace district_outcome = 4 if constituencynumber == 24001
replace district_outcome = 4 if constituencynumber == 24002
replace district_outcome = 2 if constituencynumber == 24003
replace district_outcome = 2 if constituencynumber == 24004
replace district_outcome = 2 if constituencynumber == 24005
replace district_outcome = 4 if constituencynumber == 24006
replace district_outcome = 1 if constituencynumber == 24007
replace district_outcome = 4 if constituencynumber == 24008
replace district_outcome = 4 if constituencynumber == 24009
replace district_outcome = 1 if constituencynumber == 24010
replace district_outcome = 4 if constituencynumber == 24011
replace district_outcome = 4 if constituencynumber == 24012
replace district_outcome = 4 if constituencynumber == 24013
replace district_outcome = 4 if constituencynumber == 24014
replace district_outcome = 2 if constituencynumber == 24015
replace district_outcome = 2 if constituencynumber == 24016
replace district_outcome = 2 if constituencynumber == 24017
replace district_outcome = 4 if constituencynumber == 24018
replace district_outcome = 1 if constituencynumber == 24019
replace district_outcome = 4 if constituencynumber == 24020
replace district_outcome = 2 if constituencynumber == 24021
replace district_outcome = 1 if constituencynumber == 24022
replace district_outcome = 2 if constituencynumber == 24023
replace district_outcome = 2 if constituencynumber == 24024
replace district_outcome = 4 if constituencynumber == 24025
replace district_outcome = 2 if constituencynumber == 24026
replace district_outcome = 2 if constituencynumber == 24027
replace district_outcome = 2 if constituencynumber == 24028
replace district_outcome = 2 if constituencynumber == 24029
replace district_outcome = 2 if constituencynumber == 24030
replace district_outcome = 4 if constituencynumber == 24031
replace district_outcome = 4 if constituencynumber == 24032
replace district_outcome = 4 if constituencynumber == 24033
replace district_outcome = 4 if constituencynumber == 24034
replace district_outcome = 4 if constituencynumber == 24035
replace district_outcome = 2 if constituencynumber == 24036
replace district_outcome = 2 if constituencynumber == 24037
replace district_outcome = 4 if constituencynumber == 24038
replace district_outcome = 2 if constituencynumber == 24039
replace district_outcome = 2 if constituencynumber == 24040
replace district_outcome = 2 if constituencynumber == 24041
replace district_outcome = 1 if constituencynumber == 24042
replace district_outcome = 4 if constituencynumber == 24043
replace district_outcome = 2 if constituencynumber == 24044
replace district_outcome = 1 if constituencynumber == 24045
replace district_outcome = 4 if constituencynumber == 24046
replace district_outcome = 1 if constituencynumber == 24047
replace district_outcome = 4 if constituencynumber == 24048
replace district_outcome = 4 if constituencynumber == 24049
replace district_outcome = 4 if constituencynumber == 24050
replace district_outcome = 1 if constituencynumber == 24051
replace district_outcome = 2 if constituencynumber == 24052
replace district_outcome = 2 if constituencynumber == 24053
replace district_outcome = 2 if constituencynumber == 24054
replace district_outcome = 2 if constituencynumber == 24055
replace district_outcome = 2 if constituencynumber == 24056
replace district_outcome = 2 if constituencynumber == 24057
replace district_outcome = 1 if constituencynumber == 24058
replace district_outcome = 2 if constituencynumber == 24059
replace district_outcome = 4 if constituencynumber == 24060
replace district_outcome = 1 if constituencynumber == 24061
replace district_outcome = 4 if constituencynumber == 24062
replace district_outcome = 4 if constituencynumber == 24063
replace district_outcome = 3 if constituencynumber == 24064
replace district_outcome = 2 if constituencynumber == 24065
replace district_outcome = 4 if constituencynumber == 24066
replace district_outcome = 4 if constituencynumber == 24067
replace district_outcome = 2 if constituencynumber == 24068
replace district_outcome = 2 if constituencynumber == 24069
replace district_outcome = 2 if constituencynumber == 24070
replace district_outcome = 4 if constituencynumber == 24071
replace district_outcome = 4 if constituencynumber == 24072
replace district_outcome = 2 if constituencynumber == 24073
replace district_outcome = 2 if constituencynumber == 24074
replace district_outcome = 4 if constituencynumber == 24075
replace district_outcome = 4 if constituencynumber == 24076
replace district_outcome = 2 if constituencynumber == 24077
replace district_outcome = 2 if constituencynumber == 24078
replace district_outcome = 2 if constituencynumber == 35001
replace district_outcome = 3 if constituencynumber == 35002
replace district_outcome = 1 if constituencynumber == 35003
replace district_outcome = 1 if constituencynumber == 35004
replace district_outcome = 1 if constituencynumber == 35005
replace district_outcome = 2 if constituencynumber == 35006
replace district_outcome = 2 if constituencynumber == 35007
replace district_outcome = 2 if constituencynumber == 35008
replace district_outcome = 2 if constituencynumber == 35009
replace district_outcome = 2 if constituencynumber == 35010
replace district_outcome = 2 if constituencynumber == 35011
replace district_outcome = 2 if constituencynumber == 35012
replace district_outcome = 1 if constituencynumber == 35013
replace district_outcome = 1 if constituencynumber == 35014
replace district_outcome = 2 if constituencynumber == 35015
replace district_outcome = 2 if constituencynumber == 35016
replace district_outcome = 1 if constituencynumber == 35017
replace district_outcome = 2 if constituencynumber == 35018
replace district_outcome = 2 if constituencynumber == 35019
replace district_outcome = 2 if constituencynumber == 35020
replace district_outcome = 2 if constituencynumber == 35021
replace district_outcome = 1 if constituencynumber == 35022
replace district_outcome = 1 if constituencynumber == 35023
replace district_outcome = 2 if constituencynumber == 35024
replace district_outcome = 1 if constituencynumber == 35025
replace district_outcome = 1 if constituencynumber == 35026
replace district_outcome = 2 if constituencynumber == 35027
replace district_outcome = 2 if constituencynumber == 35028
replace district_outcome = 2 if constituencynumber == 35029
replace district_outcome = 1 if constituencynumber == 35030
replace district_outcome = 2 if constituencynumber == 35031
replace district_outcome = 2 if constituencynumber == 35032
replace district_outcome = 1 if constituencynumber == 35033
replace district_outcome = 1 if constituencynumber == 35034
replace district_outcome = 3 if constituencynumber == 35035
replace district_outcome = 2 if constituencynumber == 35036
replace district_outcome = 3 if constituencynumber == 35037
replace district_outcome = 2 if constituencynumber == 35038
replace district_outcome = 1 if constituencynumber == 35039
replace district_outcome = 1 if constituencynumber == 35040
replace district_outcome = 2 if constituencynumber == 35041
replace district_outcome = 1 if constituencynumber == 35042
replace district_outcome = 2 if constituencynumber == 35043
replace district_outcome = 2 if constituencynumber == 35044
replace district_outcome = 2 if constituencynumber == 35045
replace district_outcome = 2 if constituencynumber == 35046
replace district_outcome = 2 if constituencynumber == 35047
replace district_outcome = 1 if constituencynumber == 35048
replace district_outcome = 1 if constituencynumber == 35049
replace district_outcome = 1 if constituencynumber == 35050
replace district_outcome = 3 if constituencynumber == 35051
replace district_outcome = 2 if constituencynumber == 35052
replace district_outcome = 2 if constituencynumber == 35053
replace district_outcome = 2 if constituencynumber == 35054
replace district_outcome = 2 if constituencynumber == 35055
replace district_outcome = 1 if constituencynumber == 35056
replace district_outcome = 2 if constituencynumber == 35057
replace district_outcome = 2 if constituencynumber == 35058
replace district_outcome = 2 if constituencynumber == 35059
replace district_outcome = 2 if constituencynumber == 35060
replace district_outcome = 2 if constituencynumber == 35061
replace district_outcome = 2 if constituencynumber == 35062
replace district_outcome = 2 if constituencynumber == 35063
replace district_outcome = 2 if constituencynumber == 35064
replace district_outcome = 2 if constituencynumber == 35065
replace district_outcome = 2 if constituencynumber == 35066
replace district_outcome = 1 if constituencynumber == 35067
replace district_outcome = 1 if constituencynumber == 35068
replace district_outcome = 2 if constituencynumber == 35069
replace district_outcome = 2 if constituencynumber == 35070
replace district_outcome = 1 if constituencynumber == 35071
replace district_outcome = 2 if constituencynumber == 35072
replace district_outcome = 2 if constituencynumber == 35073
replace district_outcome = 1 if constituencynumber == 35074
replace district_outcome = 2 if constituencynumber == 35075
replace district_outcome = 2 if constituencynumber == 35076
replace district_outcome = 2 if constituencynumber == 35077
replace district_outcome = 2 if constituencynumber == 35078
replace district_outcome = 2 if constituencynumber == 35079
replace district_outcome = 1 if constituencynumber == 35080
replace district_outcome = 2 if constituencynumber == 35081
replace district_outcome = 1 if constituencynumber == 35082
replace district_outcome = 1 if constituencynumber == 35083
replace district_outcome = 2 if constituencynumber == 35084
replace district_outcome = 2 if constituencynumber == 35085
replace district_outcome = 1 if constituencynumber == 35086
replace district_outcome = 2 if constituencynumber == 35087
replace district_outcome = 1 if constituencynumber == 35088
replace district_outcome = 2 if constituencynumber == 35089
replace district_outcome = 2 if constituencynumber == 35090
replace district_outcome = 1 if constituencynumber == 35091
replace district_outcome = 2 if constituencynumber == 35092
replace district_outcome = 2 if constituencynumber == 35093
replace district_outcome = 2 if constituencynumber == 35094
replace district_outcome = 2 if constituencynumber == 35095
replace district_outcome = 2 if constituencynumber == 35096
replace district_outcome = 2 if constituencynumber == 35097
replace district_outcome = 2 if constituencynumber == 35098
replace district_outcome = 1 if constituencynumber == 35099
replace district_outcome = 1 if constituencynumber == 35100
replace district_outcome = 2 if constituencynumber == 35101
replace district_outcome = 1 if constituencynumber == 35102
replace district_outcome = 2 if constituencynumber == 35103
replace district_outcome = 1 if constituencynumber == 35104
replace district_outcome = 2 if constituencynumber == 35105
replace district_outcome = 2 if constituencynumber == 35106
replace district_outcome = 3 if constituencynumber == 35107
replace district_outcome = 2 if constituencynumber == 35108
replace district_outcome = 2 if constituencynumber == 35109
replace district_outcome = 2 if constituencynumber == 35110
replace district_outcome = 2 if constituencynumber == 35111
replace district_outcome = 2 if constituencynumber == 35112
replace district_outcome = 1 if constituencynumber == 35113
replace district_outcome = 2 if constituencynumber == 35114
replace district_outcome = 2 if constituencynumber == 35115
replace district_outcome = 2 if constituencynumber == 35116
replace district_outcome = 3 if constituencynumber == 35117
replace district_outcome = 2 if constituencynumber == 35118
replace district_outcome = 1 if constituencynumber == 35119
replace district_outcome = 2 if constituencynumber == 35120
replace district_outcome = 2 if constituencynumber == 35121
replace district_outcome = 1 if constituencynumber == 46001
replace district_outcome = 1 if constituencynumber == 46002
replace district_outcome = 3 if constituencynumber == 46003
replace district_outcome = 1 if constituencynumber == 46004
replace district_outcome = 3 if constituencynumber == 46005
replace district_outcome = 1 if constituencynumber == 46006
replace district_outcome = 1 if constituencynumber == 46007
replace district_outcome = 1 if constituencynumber == 46008
replace district_outcome = 2 if constituencynumber == 46009
replace district_outcome = 1 if constituencynumber == 46010
replace district_outcome = 3 if constituencynumber == 46011
replace district_outcome = 2 if constituencynumber == 46012
replace district_outcome = 2 if constituencynumber == 46013
replace district_outcome = 2 if constituencynumber == 46014
replace district_outcome = 1 if constituencynumber == 47001
replace district_outcome = 1 if constituencynumber == 47002
replace district_outcome = 1 if constituencynumber == 47003
replace district_outcome = 1 if constituencynumber == 47004
replace district_outcome = 1 if constituencynumber == 47005
replace district_outcome = 1 if constituencynumber == 47006
replace district_outcome = 1 if constituencynumber == 47007
replace district_outcome = 1 if constituencynumber == 47008
replace district_outcome = 1 if constituencynumber == 47009
replace district_outcome = 1 if constituencynumber == 47010
replace district_outcome = 1 if constituencynumber == 47011
replace district_outcome = 1 if constituencynumber == 47012
replace district_outcome = 1 if constituencynumber == 47013
replace district_outcome = 1 if constituencynumber == 47014
replace district_outcome = 1 if constituencynumber == 48001
replace district_outcome = 1 if constituencynumber == 48002
replace district_outcome = 1 if constituencynumber == 48003
replace district_outcome = 1 if constituencynumber == 48004
replace district_outcome = 1 if constituencynumber == 48005
replace district_outcome = 1 if constituencynumber == 48006
replace district_outcome = 1 if constituencynumber == 48007
replace district_outcome = 1 if constituencynumber == 48008
replace district_outcome = 1 if constituencynumber == 48009
replace district_outcome = 1 if constituencynumber == 48010
replace district_outcome = 1 if constituencynumber == 48011
replace district_outcome = 1 if constituencynumber == 48012
replace district_outcome = 1 if constituencynumber == 48013
replace district_outcome = 1 if constituencynumber == 48014
replace district_outcome = 1 if constituencynumber == 48015
replace district_outcome = 1 if constituencynumber == 48016
replace district_outcome = 1 if constituencynumber == 48017
replace district_outcome = 1 if constituencynumber == 48018
replace district_outcome = 3 if constituencynumber == 48019
replace district_outcome = 1 if constituencynumber == 48020
replace district_outcome = 1 if constituencynumber == 48021
replace district_outcome = 1 if constituencynumber == 48022
replace district_outcome = 1 if constituencynumber == 48023
replace district_outcome = 1 if constituencynumber == 48024
replace district_outcome = 1 if constituencynumber == 48025
replace district_outcome = 1 if constituencynumber == 48026
replace district_outcome = 1 if constituencynumber == 48027
replace district_outcome = 1 if constituencynumber == 48028
replace district_outcome = 1 if constituencynumber == 48029
replace district_outcome = 1 if constituencynumber == 48030
replace district_outcome = 1 if constituencynumber == 48031
replace district_outcome = 1 if constituencynumber == 48032
replace district_outcome = 1 if constituencynumber == 48033
replace district_outcome = 1 if constituencynumber == 48034
replace district_outcome = 1 if constituencynumber == 59001
replace district_outcome = 2 if constituencynumber == 59002
replace district_outcome = 3 if constituencynumber == 59003
replace district_outcome = 1 if constituencynumber == 59004
replace district_outcome = 1 if constituencynumber == 59005
replace district_outcome = 1 if constituencynumber == 59006
replace district_outcome = 1 if constituencynumber == 59007
replace district_outcome = 2 if constituencynumber == 59008
replace district_outcome = 3 if constituencynumber == 59009
replace district_outcome = 3 if constituencynumber == 59010
replace district_outcome = 2 if constituencynumber == 59011
replace district_outcome = 2 if constituencynumber == 59012
replace district_outcome = 1 if constituencynumber == 59013
replace district_outcome = 1 if constituencynumber == 59014
replace district_outcome = 1 if constituencynumber == 59015
replace district_outcome = 1 if constituencynumber == 59016
replace district_outcome = 1 if constituencynumber == 59017
replace district_outcome = 5 if constituencynumber == 59018
replace district_outcome = 3 if constituencynumber == 59019
replace district_outcome = 1 if constituencynumber == 59020
replace district_outcome = 2 if constituencynumber == 59021
replace district_outcome = 1 if constituencynumber == 59022
replace district_outcome = 1 if constituencynumber == 59023
replace district_outcome = 1 if constituencynumber == 59024
replace district_outcome = 1 if constituencynumber == 59025
replace district_outcome = 3 if constituencynumber == 59026
replace district_outcome = 5 if constituencynumber == 59027
replace district_outcome = 3 if constituencynumber == 59028
replace district_outcome = 3 if constituencynumber == 59029
replace district_outcome = 1 if constituencynumber == 59030
replace district_outcome = 1 if constituencynumber == 59031
replace district_outcome = 2 if constituencynumber == 59032
replace district_outcome = 2 if constituencynumber == 59033
replace district_outcome = 2 if constituencynumber == 59034
replace district_outcome = 3 if constituencynumber == 59035
replace district_outcome = 88 if constituencynumber == 59036
replace district_outcome = 3 if constituencynumber == 59037
replace district_outcome = 3 if constituencynumber == 59038
replace district_outcome = 2 if constituencynumber == 59039
replace district_outcome = 2 if constituencynumber == 59040
replace district_outcome = 3 if constituencynumber == 59041
replace district_outcome = 2 if constituencynumber == 59042
replace district_outcome = 2 if constituencynumber == 60001
replace district_outcome = 2 if constituencynumber == 61001
replace district_outcome = 3 if constituencynumber == 62001

label define outcome 1 "Conservative Party of Canada" 2 "Liberal Party of Canada" 3 "New Democratic Party of Canada" 4 "Bloc Québécois" 5 "Green Party of Canada" 6 "People's Party of Canada'" 88 "Other"
label values district_outcome outcome


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 15.2 if constituencynumber == 10001
replace margin = 6.200001 if constituencynumber == 10002
replace margin = 13 if constituencynumber == 10003
replace margin = 11.4 if constituencynumber == 10004
replace margin = 19.1 if constituencynumber == 10005
replace margin = 13.7 if constituencynumber == 10006
replace margin = 24.3 if constituencynumber == 10007
replace margin = 20.3 if constituencynumber == 11001
replace margin = 21 if constituencynumber == 11002
replace margin = 5.299999 if constituencynumber == 11003
replace margin = 14.9 if constituencynumber == 11004
replace margin = 4.400002 if constituencynumber == 12001
replace margin = 16.9 if constituencynumber == 12002
replace margin = 1 if constituencynumber == 12003
replace margin = 18.3 if constituencynumber == 12004
replace margin = 12.5 if constituencynumber == 12005
replace margin = 30.2 if constituencynumber == 12006
replace margin = 18.5 if constituencynumber == 12007
replace margin = 16.3 if constituencynumber == 12008
replace margin = 13.6 if constituencynumber == 12009
replace margin = 3.199999 if constituencynumber == 12010
replace margin = 2.899998 if constituencynumber == 12011
replace margin = 33.6 if constituencynumber == 13001
replace margin = 19.9 if constituencynumber == 13002
replace margin = 3.300001 if constituencynumber == 13003
replace margin = 20.4 if constituencynumber == 13004
replace margin = 21.9 if constituencynumber == 13005
replace margin = 1.099998 if constituencynumber == 13006
replace margin = 19.5 if constituencynumber == 13007
replace margin = 23.6 if constituencynumber == 13008
replace margin = 3.400002 if constituencynumber == 13009
replace margin = 25.1 if constituencynumber == 13010
replace margin = 7.799999 if constituencynumber == 24001
replace margin = 20.7 if constituencynumber == 24002
replace margin = 30.7 if constituencynumber == 24003
replace margin = 19.3 if constituencynumber == 24004
replace margin = 1.5 if constituencynumber == 24005
replace margin = 17.5 if constituencynumber == 24006
replace margin = 10.2 if constituencynumber == 24007
replace margin = 3.900002 if constituencynumber == 24008
replace margin = 38.9 if constituencynumber == 24009
replace margin = 27.2 if constituencynumber == 24010
replace margin = 27.4 if constituencynumber == 24011
replace margin = 2.599998 if constituencynumber == 24012
replace margin = 6 if constituencynumber == 24013
replace margin = 22.5 if constituencynumber == 24014
replace margin = 35.2 if constituencynumber == 24015
replace margin = 3.799999 if constituencynumber == 24016
replace margin = 34.3 if constituencynumber == 24017
replace margin = 9.299999 if constituencynumber == 24018
replace margin = 10.8 if constituencynumber == 24019
replace margin = 6.700001 if constituencynumber == 24020
replace margin = 1.200001 if constituencynumber == 24021
replace margin = 1.899998 if constituencynumber == 24022
replace margin = 5.4 if constituencynumber == 24023
replace margin = 35.8 if constituencynumber == 24024
replace margin = 27.4 if constituencynumber == 24025
replace margin = 1.700001 if constituencynumber == 24026
replace margin = 30.7 if constituencynumber == 24027
replace margin = .7000008 if constituencynumber == 24028
replace margin = 38.9 if constituencynumber == 24029
replace margin = 39.5 if constituencynumber == 24030
replace margin = 35.7 if constituencynumber == 24031
replace margin = 11 if constituencynumber == 24032
replace margin = 16.4 if constituencynumber == 24033
replace margin = 5.200001 if constituencynumber == 24034
replace margin = 18.9 if constituencynumber == 24035
replace margin = 42.9 if constituencynumber == 24036
replace margin = 19.4 if constituencynumber == 24037
replace margin = 13.7 if constituencynumber == 24038
replace margin = 16.6 if constituencynumber == 24039
replace margin = 27.6 if constituencynumber == 24040
replace margin = 2.5 if constituencynumber == 24041
replace margin = 19.5 if constituencynumber == 24042
replace margin = 4.299999 if constituencynumber == 24043
replace margin = 12.5 if constituencynumber == 24044
replace margin = 22.3 if constituencynumber == 24045
replace margin = 34.6 if constituencynumber == 24046
replace margin = 23.4 if constituencynumber == 24047
replace margin = 26.2 if constituencynumber == 24048
replace margin = 7.200001 if constituencynumber == 24049
replace margin = 37.6 if constituencynumber == 24050
replace margin = 9.400002 if constituencynumber == 24051
replace margin = 31.4 if constituencynumber == 24052
replace margin = 40.9 if constituencynumber == 24053
replace margin = 26.1 if constituencynumber == 24054
replace margin = 31.9 if constituencynumber == 24055
replace margin = 38.7 if constituencynumber == 24056
replace margin = 32.1 if constituencynumber == 24057
replace margin = 19.2 if constituencynumber == 24058
replace margin = .5999985 if constituencynumber == 24059
replace margin = 25.5 if constituencynumber == 24060
replace margin = 17.1 if constituencynumber == 24061
replace margin = 4.5 if constituencynumber == 24062
replace margin = 29.7 if constituencynumber == 24063
replace margin = 18.3 if constituencynumber == 24064
replace margin = 12.1 if constituencynumber == 24065
replace margin = 20.1 if constituencynumber == 24066
replace margin = 14.2 if constituencynumber == 24067
replace margin = 41.2 if constituencynumber == 24068
replace margin = 49.4 if constituencynumber == 24069
replace margin = 5.399998 if constituencynumber == 24070
replace margin = 18 if constituencynumber == 24071
replace margin = 1.5 if constituencynumber == 24072
replace margin = 1 if constituencynumber == 24073
replace margin = 22.9 if constituencynumber == 24074
replace margin = 21.3 if constituencynumber == 24075
replace margin = 2.4 if constituencynumber == 24076
replace margin = 37.7 if constituencynumber == 24077
replace margin = 19.9 if constituencynumber == 24078
replace margin = 31.7 if constituencynumber == 35001
replace margin = 15.4 if constituencynumber == 35002
replace margin = 2 if constituencynumber == 35003
replace margin = 14.5 if constituencynumber == 35004
replace margin = 7.799999 if constituencynumber == 35005
replace margin = 2.400002 if constituencynumber == 35006
replace margin = 35.8 if constituencynumber == 35007
replace margin = 20.3 if constituencynumber == 35008
replace margin = 21.1 if constituencynumber == 35009
replace margin = 23.7 if constituencynumber == 35010
replace margin = 21.1 if constituencynumber == 35011
replace margin = 29.6 if constituencynumber == 35012
replace margin = 9.599998 if constituencynumber == 35013
replace margin = 16 if constituencynumber == 35014
replace margin = 15.4 if constituencynumber == 35015
replace margin = 9.5 if constituencynumber == 35016
replace margin = 15.7 if constituencynumber == 35017
replace margin = 2.700001 if constituencynumber == 35018
replace margin = 35.9 if constituencynumber == 35019
replace margin = 15 if constituencynumber == 35020
replace margin = 24.6 if constituencynumber == 35021
replace margin = 9 if constituencynumber == 35022
replace margin = 9.899998 if constituencynumber == 35023
replace margin = 20.2 if constituencynumber == 35024
replace margin = 27 if constituencynumber == 35025
replace margin = 6.800003 if constituencynumber == 35026
replace margin = 17.4 if constituencynumber == 35027
replace margin = 23.2 if constituencynumber == 35028
replace margin = 39.2 if constituencynumber == 35029
replace margin = 2.600002 if constituencynumber == 35030
replace margin = 11.6 if constituencynumber == 35031
replace margin = 14.9 if constituencynumber == 35032
replace margin = 22.3 if constituencynumber == 35033
replace margin = 23.1 if constituencynumber == 35034
replace margin = 17.5 if constituencynumber == 35035
replace margin = 9.999998 if constituencynumber == 35036
replace margin = 5.799999 if constituencynumber == 35037
replace margin = 19.9 if constituencynumber == 35038
replace margin = 4.300003 if constituencynumber == 35039
replace margin = 15.4 if constituencynumber == 35040
replace margin = 6.599998 if constituencynumber == 35041
replace margin = 4 if constituencynumber == 35042
replace margin = 1.799999 if constituencynumber == 35043
replace margin = 22.5 if constituencynumber == 35044
replace margin = 10.7 if constituencynumber == 35045
replace margin = .7000008 if constituencynumber == 35046
replace margin = 6.700001 if constituencynumber == 35047
replace margin = 23.6 if constituencynumber == 35048
replace margin = 23.4 if constituencynumber == 35049
replace margin = 22.5 if constituencynumber == 35050
replace margin = 13.9 if constituencynumber == 35051
replace margin = 19.1 if constituencynumber == 35052
replace margin = 15.1 if constituencynumber == 35053
replace margin = 8.300001 if constituencynumber == 35054
replace margin = 19.3 if constituencynumber == 35055
replace margin = 10.5 if constituencynumber == 35056
replace margin = 15.6 if constituencynumber == 35057
replace margin = 26.3 if constituencynumber == 35058
replace margin = 19.5 if constituencynumber == 35059
replace margin = 20.8 if constituencynumber == 35060
replace margin = 11.1 if constituencynumber == 35061
replace margin = 31.7 if constituencynumber == 35062
replace margin = 17.3 if constituencynumber == 35063
replace margin = 12.4 if constituencynumber == 35064
replace margin = 5.299999 if constituencynumber == 35065
replace margin = 4 if constituencynumber == 35066
replace margin = 3 if constituencynumber == 35067
replace margin = 13 if constituencynumber == 35068
replace margin = 6.900002 if constituencynumber == 35069
replace margin = 13.3 if constituencynumber == 35070
replace margin = 3.5 if constituencynumber == 35071
replace margin = 7.200001 if constituencynumber == 35072
replace margin = 10.3 if constituencynumber == 35073
replace margin = 10.4 if constituencynumber == 35074
replace margin = 19.7 if constituencynumber == 35075
replace margin = 26.1 if constituencynumber == 35076
replace margin = 27.8 if constituencynumber == 35077
replace margin = 30 if constituencynumber == 35078
replace margin = 18.5 if constituencynumber == 35079
replace margin = 27.9 if constituencynumber == 35080
replace margin = 15.9 if constituencynumber == 35081
replace margin = 11.4 if constituencynumber == 35082
replace margin = 19.2 if constituencynumber == 35083
replace margin = 4.399998 if constituencynumber == 35084
replace margin = 21.9 if constituencynumber == 35085
replace margin = 33.1 if constituencynumber == 35086
replace margin = .4000015 if constituencynumber == 35087
replace margin = 8.099998 if constituencynumber == 35088
replace margin = 8.6 if constituencynumber == 35089
replace margin = 32.7 if constituencynumber == 35090
replace margin = 27.6 if constituencynumber == 35091
replace margin = 6.899998 if constituencynumber == 35092
replace margin = 13.5 if constituencynumber == 35093
replace margin = 32.9 if constituencynumber == 35094
replace margin = 38.7 if constituencynumber == 35095
replace margin = 23.3 if constituencynumber == 35096
replace margin = 42.1 if constituencynumber == 35097
replace margin = 36.5 if constituencynumber == 35098
replace margin = 11.8 if constituencynumber == 35099
replace margin = 12.6 if constituencynumber == 35100
replace margin = 35.7 if constituencynumber == 35101
replace margin = 28.3 if constituencynumber == 35102
replace margin = 12 if constituencynumber == 35103
replace margin = 19.2 if constituencynumber == 35104
replace margin = 6 if constituencynumber == 35105
replace margin = 17.3 if constituencynumber == 35106
replace margin = 13.5 if constituencynumber == 35107
replace margin = 35.1 if constituencynumber == 35108
replace margin = 14.5 if constituencynumber == 35109
replace margin = 29.8 if constituencynumber == 35110
replace margin = 15 if constituencynumber == 35111
replace margin = 24.3 if constituencynumber == 35112
replace margin = 19 if constituencynumber == 35113
replace margin = 8.200001 if constituencynumber == 35114
replace margin = 12.8 if constituencynumber == 35115
replace margin = 1.100002 if constituencynumber == 35116
replace margin = 3.700001 if constituencynumber == 35117
replace margin = 13.5 if constituencynumber == 35118
replace margin = 19.5 if constituencynumber == 35119
replace margin = 39.5 if constituencynumber == 35120
replace margin = 42.1 if constituencynumber == 35121
replace margin = 49.4 if constituencynumber == 46001
replace margin = 5.200001 if constituencynumber == 46002
replace margin = 26.6 if constituencynumber == 46003
replace margin = 50.2 if constituencynumber == 46004
replace margin = 8.099998 if constituencynumber == 46005
replace margin = 16.9 if constituencynumber == 46006
replace margin = 60.1 if constituencynumber == 46007
replace margin = 52.8 if constituencynumber == 46008
replace margin = 10.3 if constituencynumber == 46009
replace margin = 44.8 if constituencynumber == 46010
replace margin = 7.5 if constituencynumber == 46011
replace margin = 21.7 if constituencynumber == 46012
replace margin = 3.399998 if constituencynumber == 46013
replace margin = 15.3 if constituencynumber == 46014
replace margin = 66.9 if constituencynumber == 47001
replace margin = 71.6 if constituencynumber == 47002
replace margin = 13.9 if constituencynumber == 47003
replace margin = 66.3 if constituencynumber == 47004
replace margin = 54.1 if constituencynumber == 47005
replace margin = 50.3 if constituencynumber == 47006
replace margin = 23.9 if constituencynumber == 47007
replace margin = 43.3 if constituencynumber == 47008
replace margin = 15.8 if constituencynumber == 47009
replace margin = 27.7 if constituencynumber == 47010
replace margin = 22.3 if constituencynumber == 47011
replace margin = 7.400002 if constituencynumber == 47012
replace margin = 76.7 if constituencynumber == 47013
replace margin = 64 if constituencynumber == 47014
replace margin = 60.3 if constituencynumber == 48001
replace margin = 80.4 if constituencynumber == 48002
replace margin = 78.1 if constituencynumber == 48003
replace margin = 29.6 if constituencynumber == 48004
replace margin = 32.5 if constituencynumber == 48005
replace margin = 37.9 if constituencynumber == 48006
replace margin = 56.7 if constituencynumber == 48007
replace margin = 63.3 if constituencynumber == 48008
replace margin = 54.1 if constituencynumber == 48009
replace margin = 49.9 if constituencynumber == 48010
replace margin = 63.9 if constituencynumber == 48011
replace margin = 54.7 if constituencynumber == 48012
replace margin = 24.2 if constituencynumber == 48013
replace margin = 8.400002 if constituencynumber == 48014
replace margin = 26.3 if constituencynumber == 48015
replace margin = 34.4 if constituencynumber == 48016
replace margin = 16.7 if constituencynumber == 48017
replace margin = 34.4 if constituencynumber == 48018
replace margin = 10.2 if constituencynumber == 48019
replace margin = 40.8 if constituencynumber == 48020
replace margin = 60 if constituencynumber == 48021
replace margin = 76.2 if constituencynumber == 48022
replace margin = 70.4 if constituencynumber == 48023
replace margin = 77 if constituencynumber == 48024
replace margin = 77.4 if constituencynumber == 48025
replace margin = 51.1 if constituencynumber == 48026
replace margin = 70.5 if constituencynumber == 48027
replace margin = 73.2 if constituencynumber == 48028
replace margin = 73 if constituencynumber == 48029
replace margin = 70.9 if constituencynumber == 48030
replace margin = 41.5 if constituencynumber == 48031
replace margin = 61.3 if constituencynumber == 48032
replace margin = 67.4 if constituencynumber == 48033
replace margin = 75.1 if constituencynumber == 48034
replace margin = 29.8 if constituencynumber == 59001
replace margin = 3.200001 if constituencynumber == 59002
replace margin = 6.800001 if constituencynumber == 59003
replace margin = 32.7 if constituencynumber == 59004
replace margin = 22.9 if constituencynumber == 59005
replace margin = 29.4 if constituencynumber == 59006
replace margin = 2.5 if constituencynumber == 59007
replace margin = .7000008 if constituencynumber == 59008
replace margin = 8.100002 if constituencynumber == 59009
replace margin = 10.1 if constituencynumber == 59010
replace margin = 8.200001 if constituencynumber == 59011
replace margin = 3.799999 if constituencynumber == 59012
replace margin = 17.5 if constituencynumber == 59013
replace margin = 12.9 if constituencynumber == 59014
replace margin = 10.4 if constituencynumber == 59015
replace margin = 21.4 if constituencynumber == 59016
replace margin = 15.7 if constituencynumber == 59017
replace margin = 8.699999 if constituencynumber == 59018
replace margin = 20.8 if constituencynumber == 59019
replace margin = 26.2 if constituencynumber == 59020
replace margin = 16 if constituencynumber == 59021
replace margin = 6.5 if constituencynumber == 59022
replace margin = .3000011 if constituencynumber == 59023
replace margin = 58.2 if constituencynumber == 59024
replace margin = 20.5 if constituencynumber == 59025
replace margin = 7.699999 if constituencynumber == 59026
replace margin = 28.9 if constituencynumber == 59027
replace margin = 7.700001 if constituencynumber == 59028
replace margin = 1.200001 if constituencynumber == 59029
replace margin = 4.5 if constituencynumber == 59030
replace margin = 6.600002 if constituencynumber == 59031
replace margin = 9.900002 if constituencynumber == 59032
replace margin = 15.8 if constituencynumber == 59033
replace margin = 18.5 if constituencynumber == 59034
replace margin = 34.5 if constituencynumber == 59035
replace margin = 5.999998 if constituencynumber == 59036
replace margin = 5.300003 if constituencynumber == 59037
replace margin = 26 if constituencynumber == 59038
replace margin = 15.8 if constituencynumber == 59039
replace margin = 7.900002 if constituencynumber == 59040
replace margin = 3.300001 if constituencynumber == 59041
replace margin = 8.200001 if constituencynumber == 59042
replace margin = .7999992 if constituencynumber == 60001
replace margin = 14.2 if constituencynumber == 61001
replace margin = 9.9 if constituencynumber == 62001

* Effective number of electoral parties by district

gen enep = .

replace enep = 2.907526 if constituencynumber == 10001
replace enep = 2.630783 if constituencynumber == 10002
replace enep = 2.667563 if constituencynumber == 10003
replace enep = 2.960586 if constituencynumber == 10004
replace enep = 2.895906 if constituencynumber == 10005
replace enep = 2.752773 if constituencynumber == 10006
replace enep = 2.703996 if constituencynumber == 10007
replace enep = 2.850603 if constituencynumber == 11001
replace enep = 3.285454 if constituencynumber == 11002
replace enep = 3.136074 if constituencynumber == 11003
replace enep = 3.211489 if constituencynumber == 11004
replace enep = 3.345287 if constituencynumber == 12001
replace enep = 3.038812 if constituencynumber == 12002
replace enep = 3.399406 if constituencynumber == 12003
replace enep = 3.183497 if constituencynumber == 12004
replace enep = 3.279183 if constituencynumber == 12005
replace enep = 2.995994 if constituencynumber == 12006
replace enep = 3.393028 if constituencynumber == 12007
replace enep = 3.527175 if constituencynumber == 12008
replace enep = 3.426746 if constituencynumber == 12009
replace enep = 4.247799 if constituencynumber == 12010
replace enep = 3.178599 if constituencynumber == 12011
replace enep = 2.640111 if constituencynumber == 13001
replace enep = 3.089414 if constituencynumber == 13002
replace enep = 3.509191 if constituencynumber == 13003
replace enep = 3.22409 if constituencynumber == 13004
replace enep = 2.778071 if constituencynumber == 13005
replace enep = 3.509215 if constituencynumber == 13006
replace enep = 3.4846 if constituencynumber == 13007
replace enep = 3.011676 if constituencynumber == 13008
replace enep = 3.54134 if constituencynumber == 13009
replace enep = 2.913244 if constituencynumber == 13010
replace enep = 3.899396 if constituencynumber == 24001
replace enep = 3.302194 if constituencynumber == 24002
replace enep = 2.908397 if constituencynumber == 24003
replace enep = 3.027697 if constituencynumber == 24004
replace enep = 3.36226 if constituencynumber == 24005
replace enep = 2.583525 if constituencynumber == 24006
replace enep = 3.786775 if constituencynumber == 24007
replace enep = 4.1287 if constituencynumber == 24008
replace enep = 2.611627 if constituencynumber == 24009
replace enep = 2.982929 if constituencynumber == 24010
replace enep = 2.980431 if constituencynumber == 24011
replace enep = 3.402066 if constituencynumber == 24012
replace enep = 3.135425 if constituencynumber == 24013
replace enep = 2.816235 if constituencynumber == 24014
replace enep = 2.534366 if constituencynumber == 24015
replace enep = 3.45681 if constituencynumber == 24016
replace enep = 2.837596 if constituencynumber == 24017
replace enep = 3.555733 if constituencynumber == 24018
replace enep = 3.68411 if constituencynumber == 24019
replace enep = 3.712393 if constituencynumber == 24020
replace enep = 3.271181 if constituencynumber == 24021
replace enep = 3.426676 if constituencynumber == 24022
replace enep = 3.650288 if constituencynumber == 24023
replace enep = 2.965344 if constituencynumber == 24024
replace enep = 3.507258 if constituencynumber == 24025
replace enep = 2.806608 if constituencynumber == 24026
replace enep = 2.926304 if constituencynumber == 24027
replace enep = 3.601475 if constituencynumber == 24028
replace enep = 2.497921 if constituencynumber == 24029
replace enep = 2.891527 if constituencynumber == 24030
replace enep = 2.491901 if constituencynumber == 24031
replace enep = 3.895674 if constituencynumber == 24032
replace enep = 3.032785 if constituencynumber == 24033
replace enep = 3.081541 if constituencynumber == 24034
replace enep = 3.19501 if constituencynumber == 24035
replace enep = 2.597686 if constituencynumber == 24036
replace enep = 3.51881 if constituencynumber == 24037
replace enep = 2.935177 if constituencynumber == 24038
replace enep = 3.393811 if constituencynumber == 24039
replace enep = 3.211768 if constituencynumber == 24040
replace enep = 3.279818 if constituencynumber == 24041
replace enep = 3.362882 if constituencynumber == 24042
replace enep = 3.457432 if constituencynumber == 24043
replace enep = 3.552221 if constituencynumber == 24044
replace enep = 3.346104 if constituencynumber == 24045
replace enep = 2.724231 if constituencynumber == 24046
replace enep = 2.979205 if constituencynumber == 24047
replace enep = 2.933894 if constituencynumber == 24048
replace enep = 3.082082 if constituencynumber == 24049
replace enep = 2.548992 if constituencynumber == 24050
replace enep = 3.225682 if constituencynumber == 24051
replace enep = 2.560479 if constituencynumber == 24052
replace enep = 2.721348 if constituencynumber == 24053
replace enep = 3.426394 if constituencynumber == 24054
replace enep = 3.021796 if constituencynumber == 24055
replace enep = 2.706118 if constituencynumber == 24056
replace enep = 3.244741 if constituencynumber == 24057
replace enep = 3.40791 if constituencynumber == 24058
replace enep = 3.896463 if constituencynumber == 24059
replace enep = 2.693124 if constituencynumber == 24060
replace enep = 3.196032 if constituencynumber == 24061
replace enep = 3.204687 if constituencynumber == 24062
replace enep = 2.922626 if constituencynumber == 24063
replace enep = 3.334378 if constituencynumber == 24064
replace enep = 3.11379 if constituencynumber == 24065
replace enep = 3.666576 if constituencynumber == 24066
replace enep = 3.180864 if constituencynumber == 24067
replace enep = 2.551164 if constituencynumber == 24068
replace enep = 2.447016 if constituencynumber == 24069
replace enep = 3.285421 if constituencynumber == 24070
replace enep = 3.005349 if constituencynumber == 24071
replace enep = 3.250003 if constituencynumber == 24072
replace enep = 4.055529 if constituencynumber == 24073
replace enep = 3.217979 if constituencynumber == 24074
replace enep = 2.819054 if constituencynumber == 24075
replace enep = 4.141661 if constituencynumber == 24076
replace enep = 2.930377 if constituencynumber == 24077
replace enep = 3.074359 if constituencynumber == 24078
replace enep = 2.409894 if constituencynumber == 35001
replace enep = 3.287765 if constituencynumber == 35002
replace enep = 2.604322 if constituencynumber == 35003
replace enep = 3.199805 if constituencynumber == 35004
replace enep = 3.462208 if constituencynumber == 35005
replace enep = 3.136674 if constituencynumber == 35006
replace enep = 2.521642 if constituencynumber == 35007
replace enep = 2.976049 if constituencynumber == 35008
replace enep = 2.847543 if constituencynumber == 35009
replace enep = 2.699157 if constituencynumber == 35010
replace enep = 2.82269 if constituencynumber == 35011
replace enep = 2.646203 if constituencynumber == 35012
replace enep = 3.333256 if constituencynumber == 35013
replace enep = 3.073377 if constituencynumber == 35014
replace enep = 2.767408 if constituencynumber == 35015
replace enep = 3.449073 if constituencynumber == 35016
replace enep = 2.919486 if constituencynumber == 35017
replace enep = 2.703887 if constituencynumber == 35018
replace enep = 2.33321 if constituencynumber == 35019
replace enep = 2.567941 if constituencynumber == 35020
replace enep = 2.403893 if constituencynumber == 35021
replace enep = 3.220602 if constituencynumber == 35022
replace enep = 3.150946 if constituencynumber == 35023
replace enep = 2.48351 if constituencynumber == 35024
replace enep = 2.930686 if constituencynumber == 35025
replace enep = 3.043584 if constituencynumber == 35026
replace enep = 2.522723 if constituencynumber == 35027
replace enep = 2.705159 if constituencynumber == 35028
replace enep = 2.276053 if constituencynumber == 35029
replace enep = 3.136546 if constituencynumber == 35030
replace enep = 2.714581 if constituencynumber == 35031
replace enep = 3.560328 if constituencynumber == 35032
replace enep = 3.226015 if constituencynumber == 35033
replace enep = 2.961147 if constituencynumber == 35034
replace enep = 3.098556 if constituencynumber == 35035
replace enep = 3.357136 if constituencynumber == 35036
replace enep = 3.443965 if constituencynumber == 35037
replace enep = 3.070706 if constituencynumber == 35038
replace enep = 3.025105 if constituencynumber == 35039
replace enep = 2.762965 if constituencynumber == 35040
replace enep = 2.948435 if constituencynumber == 35041
replace enep = 3.447301 if constituencynumber == 35042
replace enep = 2.530012 if constituencynumber == 35043
replace enep = 3.218798 if constituencynumber == 35044
replace enep = 3.675403 if constituencynumber == 35045
replace enep = 3.036274 if constituencynumber == 35046
replace enep = 3.292018 if constituencynumber == 35047
replace enep = 2.987804 if constituencynumber == 35048
replace enep = 3.075797 if constituencynumber == 35049
replace enep = 2.958947 if constituencynumber == 35050
replace enep = 3.297566 if constituencynumber == 35051
replace enep = 3.34295 if constituencynumber == 35052
replace enep = 3.207616 if constituencynumber == 35053
replace enep = 3.41774 if constituencynumber == 35054
replace enep = 2.401248 if constituencynumber == 35055
replace enep = 2.543144 if constituencynumber == 35056
replace enep = 2.474935 if constituencynumber == 35057
replace enep = 2.445903 if constituencynumber == 35058
replace enep = 2.477369 if constituencynumber == 35059
replace enep = 2.482967 if constituencynumber == 35060
replace enep = 2.612979 if constituencynumber == 35061
replace enep = 2.416364 if constituencynumber == 35062
replace enep = 2.656311 if constituencynumber == 35063
replace enep = 2.905794 if constituencynumber == 35064
replace enep = 2.908778 if constituencynumber == 35065
replace enep = 3.414216 if constituencynumber == 35066
replace enep = 3.678418 if constituencynumber == 35067
replace enep = 3.036883 if constituencynumber == 35068
replace enep = 3.294057 if constituencynumber == 35069
replace enep = 3.48477 if constituencynumber == 35070
replace enep = 3.176469 if constituencynumber == 35071
replace enep = 2.657652 if constituencynumber == 35072
replace enep = 2.586452 if constituencynumber == 35073
replace enep = 3.332589 if constituencynumber == 35074
replace enep = 2.91757 if constituencynumber == 35075
replace enep = 2.562907 if constituencynumber == 35076
replace enep = 2.75923 if constituencynumber == 35077
replace enep = 2.912828 if constituencynumber == 35078
replace enep = 3.119872 if constituencynumber == 35079
replace enep = 3.15861 if constituencynumber == 35080
replace enep = 2.893929 if constituencynumber == 35081
replace enep = 3.283059 if constituencynumber == 35082
replace enep = 3.148109 if constituencynumber == 35083
replace enep = 3.22193 if constituencynumber == 35084
replace enep = 2.755261 if constituencynumber == 35085
replace enep = 2.918149 if constituencynumber == 35086
replace enep = 2.604201 if constituencynumber == 35087
replace enep = 2.692051 if constituencynumber == 35088
replace enep = 3.244372 if constituencynumber == 35089
replace enep = 2.69311 if constituencynumber == 35090
replace enep = 2.963112 if constituencynumber == 35091
replace enep = 3.223031 if constituencynumber == 35092
replace enep = 2.498358 if constituencynumber == 35093
replace enep = 2.684535 if constituencynumber == 35094
replace enep = 2.287654 if constituencynumber == 35095
replace enep = 2.52378 if constituencynumber == 35096
replace enep = 2.25858 if constituencynumber == 35097
replace enep = 2.521254 if constituencynumber == 35098
replace enep = 3.168257 if constituencynumber == 35099
replace enep = 3.202193 if constituencynumber == 35100
replace enep = 2.593563 if constituencynumber == 35101
replace enep = 2.64138 if constituencynumber == 35102
replace enep = 3.354613 if constituencynumber == 35103
replace enep = 2.333319 if constituencynumber == 35104
replace enep = 3.36148 if constituencynumber == 35105
replace enep = 3.317278 if constituencynumber == 35106
replace enep = 3.275617 if constituencynumber == 35107
replace enep = 2.506806 if constituencynumber == 35108
replace enep = 2.831538 if constituencynumber == 35109
replace enep = 2.864164 if constituencynumber == 35110
replace enep = 2.487587 if constituencynumber == 35111
replace enep = 3.021413 if constituencynumber == 35112
replace enep = 3.024858 if constituencynumber == 35113
replace enep = 2.941739 if constituencynumber == 35114
replace enep = 2.622428 if constituencynumber == 35115
replace enep = 3.388613 if constituencynumber == 35116
replace enep = 3.037667 if constituencynumber == 35117
replace enep = 2.516856 if constituencynumber == 35118
replace enep = 3.178791 if constituencynumber == 35119
replace enep = 2.4471 if constituencynumber == 35120
replace enep = 2.292316 if constituencynumber == 35121
replace enep = 2.255793 if constituencynumber == 46001
replace enep = 3.165348 if constituencynumber == 46002
replace enep = 2.847162 if constituencynumber == 46003
replace enep = 2.201445 if constituencynumber == 46004
replace enep = 2.739801 if constituencynumber == 46005
replace enep = 3.074218 if constituencynumber == 46006
replace enep = 1.907931 if constituencynumber == 46007
replace enep = 2.119048 if constituencynumber == 46008
replace enep = 3.100458 if constituencynumber == 46009
replace enep = 2.254852 if constituencynumber == 46010
replace enep = 3.155261 if constituencynumber == 46011
replace enep = 2.959009 if constituencynumber == 46012
replace enep = 2.87141 if constituencynumber == 46013
replace enep = 3.066243 if constituencynumber == 46014
replace enep = 1.583978 if constituencynumber == 47001
replace enep = 1.493226 if constituencynumber == 47002
replace enep = 3.027789 if constituencynumber == 47003
replace enep = 1.572846 if constituencynumber == 47004
replace enep = 1.853286 if constituencynumber == 47005
replace enep = 1.999716 if constituencynumber == 47006
replace enep = 2.654886 if constituencynumber == 47007
replace enep = 2.21078 if constituencynumber == 47008
replace enep = 2.671896 if constituencynumber == 47009
replace enep = 2.635393 if constituencynumber == 47010
replace enep = 2.641743 if constituencynumber == 47011
replace enep = 2.52273 if constituencynumber == 47012
replace enep = 1.387912 if constituencynumber == 47013
replace enep = 1.663938 if constituencynumber == 47014
replace enep = 1.882778 if constituencynumber == 48001
replace enep = 1.357383 if constituencynumber == 48002
replace enep = 1.405904 if constituencynumber == 48003
replace enep = 2.468368 if constituencynumber == 48004
replace enep = 2.667407 if constituencynumber == 48005
replace enep = 2.406693 if constituencynumber == 48006
replace enep = 1.889141 if constituencynumber == 48007
replace enep = 1.740511 if constituencynumber == 48008
replace enep = 1.914777 if constituencynumber == 48009
replace enep = 1.96554 if constituencynumber == 48010
replace enep = 1.712992 if constituencynumber == 48011
replace enep = 1.915643 if constituencynumber == 48012
replace enep = 2.642476 if constituencynumber == 48013
replace enep = 3.089719 if constituencynumber == 48014
replace enep = 2.793327 if constituencynumber == 48015
replace enep = 2.559921 if constituencynumber == 48016
replace enep = 2.623226 if constituencynumber == 48017
replace enep = 2.458077 if constituencynumber == 48018
replace enep = 2.662974 if constituencynumber == 48019
replace enep = 2.306358 if constituencynumber == 48020
replace enep = 1.809022 if constituencynumber == 48021
replace enep = 1.464523 if constituencynumber == 48022
replace enep = 1.533617 if constituencynumber == 48023
replace enep = 1.401068 if constituencynumber == 48024
replace enep = 1.40611 if constituencynumber == 48025
replace enep = 2.107895 if constituencynumber == 48026
replace enep = 1.561575 if constituencynumber == 48027
replace enep = 1.509924 if constituencynumber == 48028
replace enep = 1.525099 if constituencynumber == 48029
replace enep = 1.540118 if constituencynumber == 48030
replace enep = 2.328945 if constituencynumber == 48031
replace enep = 1.771724 if constituencynumber == 48032
replace enep = 1.621568 if constituencynumber == 48033
replace enep = 1.464176 if constituencynumber == 48034
replace enep = 2.893309 if constituencynumber == 59001
replace enep = 3.600606 if constituencynumber == 59002
replace enep = 3.361333 if constituencynumber == 59003
replace enep = 2.855136 if constituencynumber == 59004
replace enep = 3.060921 if constituencynumber == 59005
replace enep = 3.072962 if constituencynumber == 59006
replace enep = 3.26649 if constituencynumber == 59007
replace enep = 3.403467 if constituencynumber == 59008
replace enep = 3.208265 if constituencynumber == 59009
replace enep = 3.788539 if constituencynumber == 59010
replace enep = 3.229933 if constituencynumber == 59011
replace enep = 3.282639 if constituencynumber == 59012
replace enep = 3.252255 if constituencynumber == 59013
replace enep = 2.980981 if constituencynumber == 59014
replace enep = 2.975951 if constituencynumber == 59015
replace enep = 3.114353 if constituencynumber == 59016
replace enep = 3.396139 if constituencynumber == 59017
replace enep = 3.831961 if constituencynumber == 59018
replace enep = 3.289885 if constituencynumber == 59019
replace enep = 3.080772 if constituencynumber == 59020
replace enep = 3.343453 if constituencynumber == 59021
replace enep = 3.533269 if constituencynumber == 59022
replace enep = 3.534618 if constituencynumber == 59023
replace enep = 1.945082 if constituencynumber == 59024
replace enep = 2.887737 if constituencynumber == 59025
replace enep = 3.924986 if constituencynumber == 59026
replace enep = 3.069688 if constituencynumber == 59027
replace enep = 3.346138 if constituencynumber == 59028
replace enep = 3.407457 if constituencynumber == 59029
replace enep = 2.984656 if constituencynumber == 59030
replace enep = 3.076421 if constituencynumber == 59031
replace enep = 3.517708 if constituencynumber == 59032
replace enep = 3.001705 if constituencynumber == 59033
replace enep = 3.480016 if constituencynumber == 59034
replace enep = 2.89553 if constituencynumber == 59035
replace enep = 4.084651 if constituencynumber == 59036
replace enep = 3.480573 if constituencynumber == 59037
replace enep = 2.959307 if constituencynumber == 59038
replace enep = 3.287268 if constituencynumber == 59039
replace enep = 3.138515 if constituencynumber == 59040
replace enep = 3.767656 if constituencynumber == 59041
replace enep = 3.804364 if constituencynumber == 59042
replace enep = 3.587122 if constituencynumber == 60001
replace enep = 3.522082 if constituencynumber == 61001
replace enep = 3.025261 if constituencynumber == 62001

* Competition measure

gen competition = .

replace competition = .4283795 if constituencynumber == 10001
replace competition = .2522675 if constituencynumber == 10002
replace competition = .3003395 if constituencynumber == 10003
replace competition = .325148 if constituencynumber == 10004
replace competition = .3233915 if constituencynumber == 10005
replace competition = .119774 if constituencynumber == 10006
replace competition = .2300315 if constituencynumber == 10007
replace competition = .389268 if constituencynumber == 11001
replace competition = .39063 if constituencynumber == 11002
replace competition = .3456155 if constituencynumber == 11003
replace competition = .3995195 if constituencynumber == 11004
replace competition = .31928 if constituencynumber == 12001
replace competition = .3663995 if constituencynumber == 12002
replace competition = .331228 if constituencynumber == 12003
replace competition = .369294 if constituencynumber == 12004
replace competition = .270212 if constituencynumber == 12005
replace competition = .357988 if constituencynumber == 12006
replace competition = .3380255 if constituencynumber == 12007
replace competition = .29711 if constituencynumber == 12008
replace competition = .3833255 if constituencynumber == 12009
replace competition = .3297835 if constituencynumber == 12010
replace competition = .3384015 if constituencynumber == 12011
replace competition = .2200335 if constituencynumber == 13001
replace competition = .3585955 if constituencynumber == 13002
replace competition = .3779935 if constituencynumber == 13003
replace competition = .269632 if constituencynumber == 13004
replace competition = .358264 if constituencynumber == 13005
replace competition = .29902 if constituencynumber == 13006
replace competition = .3894455 if constituencynumber == 13007
replace competition = .2515495 if constituencynumber == 13008
replace competition = .3365335 if constituencynumber == 13009
replace competition = .272852 if constituencynumber == 13010
replace competition = .3593135 if constituencynumber == 24001
replace competition = .3766535 if constituencynumber == 24002
replace competition = .37445 if constituencynumber == 24003
replace competition = .4315495 if constituencynumber == 24004
replace competition = .4248315 if constituencynumber == 24005
replace competition = .4749755 if constituencynumber == 24006
replace competition = .397216 if constituencynumber == 24007
replace competition = .4140035 if constituencynumber == 24008
replace competition = .4243455 if constituencynumber == 24009
replace competition = .44421 if constituencynumber == 24010
replace competition = .355294 if constituencynumber == 24011
replace competition = .410686 if constituencynumber == 24012
replace competition = .396682 if constituencynumber == 24013
replace competition = .4009615 if constituencynumber == 24014
replace competition = .453808 if constituencynumber == 24015
replace competition = .430356 if constituencynumber == 24016
replace competition = .4164975 if constituencynumber == 24017
replace competition = .3900295 if constituencynumber == 24018
replace competition = .435274 if constituencynumber == 24019
replace competition = .409816 if constituencynumber == 24020
replace competition = .4200735 if constituencynumber == 24021
replace competition = .37944 if constituencynumber == 24022
replace competition = .4060935 if constituencynumber == 24023
replace competition = .4248035 if constituencynumber == 24024
replace competition = .404972 if constituencynumber == 24025
replace competition = .335696 if constituencynumber == 24026
replace competition = .388838 if constituencynumber == 24027
replace competition = .35123 if constituencynumber == 24028
replace competition = .4382055 if constituencynumber == 24029
replace competition = .3461435 if constituencynumber == 24030
replace competition = .3794675 if constituencynumber == 24031
replace competition = .3838055 if constituencynumber == 24032
replace competition = .366822 if constituencynumber == 24033
replace competition = .4097895 if constituencynumber == 24034
replace competition = .392524 if constituencynumber == 24035
replace competition = .3859955 if constituencynumber == 24036
replace competition = .3868295 if constituencynumber == 24037
replace competition = .354948 if constituencynumber == 24038
replace competition = .378228 if constituencynumber == 24039
replace competition = .4483015 if constituencynumber == 24040
replace competition = .401306 if constituencynumber == 24041
replace competition = .436038 if constituencynumber == 24042
replace competition = .348464 if constituencynumber == 24043
replace competition = .408886 if constituencynumber == 24044
replace competition = .4340655 if constituencynumber == 24045
replace competition = .3845675 if constituencynumber == 24046
replace competition = .3929735 if constituencynumber == 24047
replace competition = .353088 if constituencynumber == 24048
replace competition = .3861735 if constituencynumber == 24049
replace competition = .3972995 if constituencynumber == 24050
replace competition = .38205 if constituencynumber == 24051
replace competition = .250656 if constituencynumber == 24052
replace competition = .4003175 if constituencynumber == 24053
replace competition = .347096 if constituencynumber == 24054
replace competition = .4062075 if constituencynumber == 24055
replace competition = .3959235 if constituencynumber == 24056
replace competition = .412792 if constituencynumber == 24057
replace competition = .4328415 if constituencynumber == 24058
replace competition = .4060755 if constituencynumber == 24059
replace competition = .404776 if constituencynumber == 24060
replace competition = .4181295 if constituencynumber == 24061
replace competition = .3651295 if constituencynumber == 24062
replace competition = .3529755 if constituencynumber == 24063
replace competition = .4294415 if constituencynumber == 24064
replace competition = .42819 if constituencynumber == 24065
replace competition = .3921035 if constituencynumber == 24066
replace competition = .3710795 if constituencynumber == 24067
replace competition = .3977915 if constituencynumber == 24068
replace competition = .3975155 if constituencynumber == 24069
replace competition = .4529415 if constituencynumber == 24070
replace competition = .350646 if constituencynumber == 24071
replace competition = .4309695 if constituencynumber == 24072
replace competition = .3817 if constituencynumber == 24073
replace competition = .4414615 if constituencynumber == 24074
replace competition = .378744 if constituencynumber == 24075
replace competition = .378822 if constituencynumber == 24076
replace competition = .434202 if constituencynumber == 24077
replace competition = .453156 if constituencynumber == 24078
replace competition = .2748735 if constituencynumber == 35001
replace competition = .28145 if constituencynumber == 35002
replace competition = .1217855 if constituencynumber == 35003
replace competition = .2717955 if constituencynumber == 35004
replace competition = .2106015 if constituencynumber == 35005
replace competition = .294932 if constituencynumber == 35006
replace competition = .33677 if constituencynumber == 35007
replace competition = .3027455 if constituencynumber == 35008
replace competition = .362036 if constituencynumber == 35009
replace competition = .308162 if constituencynumber == 35010
replace competition = .2810135 if constituencynumber == 35011
replace competition = .324252 if constituencynumber == 35012
replace competition = .323896 if constituencynumber == 35013
replace competition = .2404715 if constituencynumber == 35014
replace competition = .1779235 if constituencynumber == 35015
replace competition = .25109 if constituencynumber == 35016
replace competition = .2585495 if constituencynumber == 35017
replace competition = .2048355 if constituencynumber == 35018
replace competition = .33059 if constituencynumber == 35019
replace competition = .242566 if constituencynumber == 35020
replace competition = .239266 if constituencynumber == 35021
replace competition = .24333 if constituencynumber == 35022
replace competition = .2823255 if constituencynumber == 35023
replace competition = .1691535 if constituencynumber == 35024
replace competition = .339704 if constituencynumber == 35025
replace competition = .2698915 if constituencynumber == 35026
replace competition = .2462835 if constituencynumber == 35027
replace competition = .3119935 if constituencynumber == 35028
replace competition = .358438 if constituencynumber == 35029
replace competition = .238758 if constituencynumber == 35030
replace competition = .2590255 if constituencynumber == 35031
replace competition = .403208 if constituencynumber == 35032
replace competition = .2860255 if constituencynumber == 35033
replace competition = .32783 if constituencynumber == 35034
replace competition = .32781 if constituencynumber == 35035
replace competition = .2962674 if constituencynumber == 35036
replace competition = .294302 if constituencynumber == 35037
replace competition = .3309455 if constituencynumber == 35038
replace competition = .1924255 if constituencynumber == 35039
replace competition = .225478 if constituencynumber == 35040
replace competition = .2221455 if constituencynumber == 35041
replace competition = .260018 if constituencynumber == 35042
replace competition = .141588 if constituencynumber == 35043
replace competition = .3920775 if constituencynumber == 35044
replace competition = .345998 if constituencynumber == 35045
replace competition = .1829515 if constituencynumber == 35046
replace competition = .283124 if constituencynumber == 35047
replace competition = .346518 if constituencynumber == 35048
replace competition = .3107615 if constituencynumber == 35049
replace competition = .212166 if constituencynumber == 35050
replace competition = .304038 if constituencynumber == 35051
replace competition = .334922 if constituencynumber == 35052
replace competition = .2931495 if constituencynumber == 35053
replace competition = .16342 if constituencynumber == 35054
replace competition = .299778 if constituencynumber == 35055
replace competition = .1534915 if constituencynumber == 35056
replace competition = .218298 if constituencynumber == 35057
replace competition = .2914295 if constituencynumber == 35058
replace competition = .268436 if constituencynumber == 35059
replace competition = .2234755 if constituencynumber == 35060
replace competition = .1998895 if constituencynumber == 35061
replace competition = .3487575 if constituencynumber == 35062
replace competition = .212522 if constituencynumber == 35063
replace competition = .2679895 if constituencynumber == 35064
replace competition = .18177 if constituencynumber == 35065
replace competition = .28704 if constituencynumber == 35066
replace competition = .28745 if constituencynumber == 35067
replace competition = .3357675 if constituencynumber == 35068
replace competition = .252946 if constituencynumber == 35069
replace competition = .342924 if constituencynumber == 35070
replace competition = .2297175 if constituencynumber == 35071
replace competition = .1689395 if constituencynumber == 35072
replace competition = .162322 if constituencynumber == 35073
replace competition = .2936835 if constituencynumber == 35074
replace competition = .252696 if constituencynumber == 35075
replace competition = .30687 if constituencynumber == 35076
replace competition = .3681141 if constituencynumber == 35077
replace competition = .393524 if constituencynumber == 35078
replace competition = .3348875 if constituencynumber == 35079
replace competition = .3362375 if constituencynumber == 35080
replace competition = .228796 if constituencynumber == 35081
replace competition = .254602 if constituencynumber == 35082
replace competition = .2665095 if constituencynumber == 35083
replace competition = .2843995 if constituencynumber == 35084
replace competition = .2403455 if constituencynumber == 35085
replace competition = .3453475 if constituencynumber == 35086
replace competition = .158996 if constituencynumber == 35087
replace competition = .15493 if constituencynumber == 35088
replace competition = .255876 if constituencynumber == 35089
replace competition = .3576375 if constituencynumber == 35090
replace competition = .3034675 if constituencynumber == 35091
replace competition = .3217355 if constituencynumber == 35092
replace competition = .2390375 if constituencynumber == 35093
replace competition = .324584 if constituencynumber == 35094
replace competition = .3439575 if constituencynumber == 35095
replace competition = .35202 if constituencynumber == 35096
replace competition = .335624 if constituencynumber == 35097
replace competition = .37395 if constituencynumber == 35098
replace competition = .250376 if constituencynumber == 35099
replace competition = .2401715 if constituencynumber == 35100
replace competition = .354214 if constituencynumber == 35101
replace competition = .233196 if constituencynumber == 35102
replace competition = .359348 if constituencynumber == 35103
replace competition = .274804 if constituencynumber == 35104
replace competition = .3504075 if constituencynumber == 35105
replace competition = .428878 if constituencynumber == 35106
replace competition = .283788 if constituencynumber == 35107
replace competition = .3568395 if constituencynumber == 35108
replace competition = .2389215 if constituencynumber == 35109
replace competition = .357946 if constituencynumber == 35110
replace competition = .145386 if constituencynumber == 35111
replace competition = .320112 if constituencynumber == 35112
replace competition = .269186 if constituencynumber == 35113
replace competition = .1873795 if constituencynumber == 35114
replace competition = .250934 if constituencynumber == 35115
replace competition = .339588 if constituencynumber == 35116
replace competition = .3678275 if constituencynumber == 35117
replace competition = .1465735 if constituencynumber == 35118
replace competition = .24878 if constituencynumber == 35119
replace competition = .34532 if constituencynumber == 35120
replace competition = .3503655 if constituencynumber == 35121
replace competition = .259912 if constituencynumber == 46001
replace competition = .222468 if constituencynumber == 46002
replace competition = .189722 if constituencynumber == 46003
replace competition = .38657 if constituencynumber == 46004
replace competition = .2530975 if constituencynumber == 46005
replace competition = .2284655 if constituencynumber == 46006
replace competition = .359686 if constituencynumber == 46007
replace competition = .274466 if constituencynumber == 46008
replace competition = .3321275 if constituencynumber == 46009
replace competition = .3362055 if constituencynumber == 46010
replace competition = .3623435 if constituencynumber == 46011
replace competition = .35147 if constituencynumber == 46012
replace competition = .261452 if constituencynumber == 46013
replace competition = .3337175 if constituencynumber == 46014
replace competition = .393794 if constituencynumber == 47001
replace competition = .3513675 if constituencynumber == 47002
replace competition = .2478035 if constituencynumber == 47003
replace competition = .364822 if constituencynumber == 47004
replace competition = .3750595 if constituencynumber == 47005
replace competition = .3442355 if constituencynumber == 47006
replace competition = .2539235 if constituencynumber == 47007
replace competition = .3275535 if constituencynumber == 47008
replace competition = .326996 if constituencynumber == 47009
replace competition = .312054 if constituencynumber == 47010
replace competition = .303084 if constituencynumber == 47011
replace competition = .2979755 if constituencynumber == 47012
replace competition = .345612 if constituencynumber == 47013
replace competition = .380406 if constituencynumber == 47014
replace competition = .3355575 if constituencynumber == 48001
replace competition = .2647175 if constituencynumber == 48002
replace competition = .2976995 if constituencynumber == 48003
replace competition = .123238 if constituencynumber == 48004
replace competition = .161124 if constituencynumber == 48005
replace competition = .292574 if constituencynumber == 48006
replace competition = .332774 if constituencynumber == 48007
replace competition = .3397835 if constituencynumber == 48008
replace competition = .3475495 if constituencynumber == 48009
replace competition = .2897275 if constituencynumber == 48010
replace competition = .32951 if constituencynumber == 48011
replace competition = .306218 if constituencynumber == 48012
replace competition = .2378095 if constituencynumber == 48013
replace competition = .273288 if constituencynumber == 48014
replace competition = .30241 if constituencynumber == 48015
replace competition = .35554 if constituencynumber == 48016
replace competition = .2164716 if constituencynumber == 48017
replace competition = .3391395 if constituencynumber == 48018
replace competition = .3336056 if constituencynumber == 48019
replace competition = .289428 if constituencynumber == 48020
replace competition = .3529515 if constituencynumber == 48021
replace competition = .3149835 if constituencynumber == 48022
replace competition = .330116 if constituencynumber == 48023
replace competition = .333134 if constituencynumber == 48024
replace competition = .3336195 if constituencynumber == 48025
replace competition = .3943935 if constituencynumber == 48026
replace competition = .3525075 if constituencynumber == 48027
replace competition = .354098 if constituencynumber == 48028
replace competition = .3235355 if constituencynumber == 48029
replace competition = .3437015 if constituencynumber == 48030
replace competition = .4313875 if constituencynumber == 48031
replace competition = .3779395 if constituencynumber == 48032
replace competition = .350228 if constituencynumber == 48033
replace competition = .3399975 if constituencynumber == 48034
replace competition = .3247735 if constituencynumber == 59001
replace competition = .3338775 if constituencynumber == 59002
replace competition = .276394 if constituencynumber == 59003
replace competition = .3227135 if constituencynumber == 59004
replace competition = .261404 if constituencynumber == 59005
replace competition = .3165975 if constituencynumber == 59006
replace competition = .2989075 if constituencynumber == 59007
replace competition = .3067115 if constituencynumber == 59008
replace competition = .3821175 if constituencynumber == 59009
replace competition = .4197295 if constituencynumber == 59010
replace competition = .3155975 if constituencynumber == 59011
replace competition = .339212 if constituencynumber == 59012
replace competition = .2923355 if constituencynumber == 59013
replace competition = .212382 if constituencynumber == 59014
replace competition = .274644 if constituencynumber == 59015
replace competition = .281754 if constituencynumber == 59016
replace competition = .3035795 if constituencynumber == 59017
replace competition = .4144175 if constituencynumber == 59018
replace competition = .3739995 if constituencynumber == 59019
replace competition = .3343 if constituencynumber == 59020
replace competition = .36692 if constituencynumber == 59021
replace competition = .2970735 if constituencynumber == 59022
replace competition = .2968675 if constituencynumber == 59023
replace competition = .398048 if constituencynumber == 59024
replace competition = .200684 if constituencynumber == 59025
replace competition = .3945 if constituencynumber == 59026
replace competition = .4221775 if constituencynumber == 59027
replace competition = .3897295 if constituencynumber == 59028
replace competition = .3225575 if constituencynumber == 59029
replace competition = .2037355 if constituencynumber == 59030
replace competition = .25128 if constituencynumber == 59031
replace competition = .351334 if constituencynumber == 59032
replace competition = .3582915 if constituencynumber == 59033
replace competition = .4102455 if constituencynumber == 59034
replace competition = .3874075 if constituencynumber == 59035
replace competition = .348664 if constituencynumber == 59036
replace competition = .378414 if constituencynumber == 59037
replace competition = .3711255 if constituencynumber == 59038
replace competition = .3629495 if constituencynumber == 59039
replace competition = .3059415 if constituencynumber == 59040
replace competition = .34728 if constituencynumber == 59041
replace competition = .383914 if constituencynumber == 59042
replace competition = .37206 if constituencynumber == 60001
replace competition = .334562 if constituencynumber == 61001
replace competition = .3455175 if constituencynumber == 62001


*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen reelected = .

replace reelected = 1 if constituencynumber == 10001
replace reelected = 1 if constituencynumber == 10002
replace reelected = 1 if constituencynumber == 10003
replace reelected = 1 if constituencynumber == 10004
replace reelected = 1 if constituencynumber == 10005
replace reelected = 0 if constituencynumber == 10006
replace reelected = 1 if constituencynumber == 10007
replace reelected = 1 if constituencynumber == 11001
replace reelected = 1 if constituencynumber == 11002
replace reelected = 1 if constituencynumber == 11003
replace reelected = 1 if constituencynumber == 11004
replace reelected = 1 if constituencynumber == 12001
replace reelected = 1 if constituencynumber == 12002
replace reelected = 1 if constituencynumber == 12003
replace reelected = 1 if constituencynumber == 12004
replace reelected = 1 if constituencynumber == 12005
replace reelected = 1 if constituencynumber == 12006
replace reelected = 1 if constituencynumber == 12007
replace reelected = 1 if constituencynumber == 12008
replace reelected = 1 if constituencynumber == 12009
replace reelected = 1 if constituencynumber == 12010
replace reelected = 0 if constituencynumber == 12011
replace reelected = 1 if constituencynumber == 13001
replace reelected = 1 if constituencynumber == 13002
replace reelected = 0 if constituencynumber == 13003
replace reelected = 0 if constituencynumber == 13004
replace reelected = 1 if constituencynumber == 13005
replace reelected = 1 if constituencynumber == 13006
replace reelected = 1 if constituencynumber == 13007
replace reelected = 0 if constituencynumber == 13008
replace reelected = 1 if constituencynumber == 13009
replace reelected = 0 if constituencynumber == 13010
replace reelected = 0 if constituencynumber == 24001
replace reelected = 0 if constituencynumber == 24002
replace reelected = 1 if constituencynumber == 24003
replace reelected = 1 if constituencynumber == 24004
replace reelected = 1 if constituencynumber == 24005
replace reelected = 0 if constituencynumber == 24006
replace reelected = 1 if constituencynumber == 24007
replace reelected = 0 if constituencynumber == 24008
replace reelected = 1 if constituencynumber == 24009
replace reelected = 1 if constituencynumber == 24010
replace reelected = 0 if constituencynumber == 24011
replace reelected = 0 if constituencynumber == 24012
replace reelected = 0 if constituencynumber == 24013
replace reelected = 1 if constituencynumber == 24014
replace reelected = 1 if constituencynumber == 24015
replace reelected = 1 if constituencynumber == 24016
replace reelected = 1 if constituencynumber == 24017
replace reelected = 0 if constituencynumber == 24018
replace reelected = 1 if constituencynumber == 24019
replace reelected = 0 if constituencynumber == 24020
replace reelected = 1 if constituencynumber == 24021
replace reelected = 0 if constituencynumber == 24022
replace reelected = 1 if constituencynumber == 24023
replace reelected = 1 if constituencynumber == 24024
replace reelected = 0 if constituencynumber == 24025
replace reelected = 1 if constituencynumber == 24026
replace reelected = 1 if constituencynumber == 24027
replace reelected = 0 if constituencynumber == 24028
replace reelected = 1 if constituencynumber == 24029
replace reelected = 1 if constituencynumber == 24030
replace reelected = 1 if constituencynumber == 24031
replace reelected = 0 if constituencynumber == 24032
replace reelected = 1 if constituencynumber == 24033
replace reelected = 0 if constituencynumber == 24034
replace reelected = 0 if constituencynumber == 24035
replace reelected = 1 if constituencynumber == 24036
replace reelected = 1 if constituencynumber == 24037
replace reelected = 0 if constituencynumber == 24038
replace reelected = 0 if constituencynumber == 24039
replace reelected = 1 if constituencynumber == 24040
replace reelected = 1 if constituencynumber == 24041
replace reelected = 1 if constituencynumber == 24042
replace reelected = 0 if constituencynumber == 24043
replace reelected = 1 if constituencynumber == 24044
replace reelected = 1 if constituencynumber == 24045
replace reelected = 1 if constituencynumber == 24046
replace reelected = 1 if constituencynumber == 24047
replace reelected = 1 if constituencynumber == 24048
replace reelected = 0 if constituencynumber == 24049
replace reelected = 1 if constituencynumber == 24050
replace reelected = 1 if constituencynumber == 24051
replace reelected = 1 if constituencynumber == 24052
replace reelected = 1 if constituencynumber == 24053
replace reelected = 0 if constituencynumber == 24054
replace reelected = 1 if constituencynumber == 24055
replace reelected = 1 if constituencynumber == 24056
replace reelected = 1 if constituencynumber == 24057
replace reelected = 1 if constituencynumber == 24058
replace reelected = 1 if constituencynumber == 24059
replace reelected = 1 if constituencynumber == 24060
replace reelected = 1 if constituencynumber == 24061
replace reelected = 0 if constituencynumber == 24062
replace reelected = 1 if constituencynumber == 24063
replace reelected = 1 if constituencynumber == 24064
replace reelected = 1 if constituencynumber == 24065
replace reelected = 0 if constituencynumber == 24066
replace reelected = 0 if constituencynumber == 24067
replace reelected = 1 if constituencynumber == 24068
replace reelected = 1 if constituencynumber == 24069
replace reelected = 1 if constituencynumber == 24070
replace reelected = 0 if constituencynumber == 24071
replace reelected = 0 if constituencynumber == 24072
replace reelected = 0 if constituencynumber == 24073
replace reelected = 1 if constituencynumber == 24074
replace reelected = 1 if constituencynumber == 24075
replace reelected = 0 if constituencynumber == 24076
replace reelected = 1 if constituencynumber == 24077
replace reelected = 1 if constituencynumber == 24078
replace reelected = 1 if constituencynumber == 35001
replace reelected = 1 if constituencynumber == 35002
replace reelected = 0 if constituencynumber == 35003
replace reelected = 1 if constituencynumber == 35004
replace reelected = 1 if constituencynumber == 35005
replace reelected = 1 if constituencynumber == 35006
replace reelected = 1 if constituencynumber == 35007
replace reelected = 1 if constituencynumber == 35008
replace reelected = 1 if constituencynumber == 35009
replace reelected = 1 if constituencynumber == 35010
replace reelected = 1 if constituencynumber == 35011
replace reelected = 1 if constituencynumber == 35012
replace reelected = 1 if constituencynumber == 35013
replace reelected = 1 if constituencynumber == 35014
replace reelected = 1 if constituencynumber == 35015
replace reelected = 1 if constituencynumber == 35016
replace reelected = 1 if constituencynumber == 35017
replace reelected = 1 if constituencynumber == 35018
replace reelected = 1 if constituencynumber == 35019
replace reelected = 1 if constituencynumber == 35020
replace reelected = 1 if constituencynumber == 35021
replace reelected = 1 if constituencynumber == 35022
replace reelected = 1 if constituencynumber == 35023
replace reelected = 1 if constituencynumber == 35024
replace reelected = 1 if constituencynumber == 35025
replace reelected = 0 if constituencynumber == 35026
replace reelected = 1 if constituencynumber == 35027
replace reelected = 1 if constituencynumber == 35028
replace reelected = 1 if constituencynumber == 35029
replace reelected = 1 if constituencynumber == 35030
replace reelected = 1 if constituencynumber == 35031
replace reelected = 1 if constituencynumber == 35032
replace reelected = 1 if constituencynumber == 35033
replace reelected = 1 if constituencynumber == 35034
replace reelected = 1 if constituencynumber == 35035
replace reelected = 1 if constituencynumber == 35036
replace reelected = 1 if constituencynumber == 35037
replace reelected = 1 if constituencynumber == 35038
replace reelected = 0 if constituencynumber == 35039
replace reelected = 1 if constituencynumber == 35040
replace reelected = 1 if constituencynumber == 35041
replace reelected = 0 if constituencynumber == 35042
replace reelected = 1 if constituencynumber == 35043
replace reelected = 1 if constituencynumber == 35044
replace reelected = 1 if constituencynumber == 35045
replace reelected = 0 if constituencynumber == 35046
replace reelected = 1 if constituencynumber == 35047
replace reelected = 1 if constituencynumber == 35048
replace reelected = 1 if constituencynumber == 35049
replace reelected = 1 if constituencynumber == 35050
replace reelected = 1 if constituencynumber == 35051
replace reelected = 1 if constituencynumber == 35052
replace reelected = 1 if constituencynumber == 35053
replace reelected = 1 if constituencynumber == 35054
replace reelected = 1 if constituencynumber == 35055
replace reelected = 1 if constituencynumber == 35056
replace reelected = 0 if constituencynumber == 35057
replace reelected = 1 if constituencynumber == 35058
replace reelected = 1 if constituencynumber == 35059
replace reelected = 1 if constituencynumber == 35060
replace reelected = 1 if constituencynumber == 35061
replace reelected = 1 if constituencynumber == 35062
replace reelected = 1 if constituencynumber == 35063
replace reelected = 1 if constituencynumber == 35064
replace reelected = 1 if constituencynumber == 35065
replace reelected = 1 if constituencynumber == 35066
replace reelected = 1 if constituencynumber == 35067
replace reelected = 1 if constituencynumber == 35068
replace reelected = 1 if constituencynumber == 35069
replace reelected = 1 if constituencynumber == 35070
replace reelected = 0 if constituencynumber == 35071
replace reelected = 1 if constituencynumber == 35072
replace reelected = 1 if constituencynumber == 35073
replace reelected = 1 if constituencynumber == 35074
replace reelected = 1 if constituencynumber == 35075
replace reelected = 1 if constituencynumber == 35076
replace reelected = 1 if constituencynumber == 35077
replace reelected = 1 if constituencynumber == 35078
replace reelected = 1 if constituencynumber == 35079
replace reelected = 1 if constituencynumber == 35080
replace reelected = 1 if constituencynumber == 35081
replace reelected = 1 if constituencynumber == 35082
replace reelected = 1 if constituencynumber == 35083
replace reelected = 1 if constituencynumber == 35084
replace reelected = 1 if constituencynumber == 35085
replace reelected = 1 if constituencynumber == 35086
replace reelected = 1 if constituencynumber == 35087
replace reelected = 1 if constituencynumber == 35088
replace reelected = 1 if constituencynumber == 35089
replace reelected = 1 if constituencynumber == 35090
replace reelected = 1 if constituencynumber == 35091
replace reelected = 1 if constituencynumber == 35092
replace reelected = 1 if constituencynumber == 35093
replace reelected = 1 if constituencynumber == 35094
replace reelected = 1 if constituencynumber == 35095
replace reelected = 1 if constituencynumber == 35096
replace reelected = 1 if constituencynumber == 35097
replace reelected = 1 if constituencynumber == 35098
replace reelected = 1 if constituencynumber == 35099
replace reelected = 1 if constituencynumber == 35100
replace reelected = 1 if constituencynumber == 35101
replace reelected = 1 if constituencynumber == 35102
replace reelected = 1 if constituencynumber == 35103
replace reelected = 1 if constituencynumber == 35104
replace reelected = 1 if constituencynumber == 35105
replace reelected = 1 if constituencynumber == 35106
replace reelected = 1 if constituencynumber == 35107
replace reelected = 1 if constituencynumber == 35108
replace reelected = 1 if constituencynumber == 35109
replace reelected = 1 if constituencynumber == 35110
replace reelected = 1 if constituencynumber == 35111
replace reelected = 1 if constituencynumber == 35112
replace reelected = 1 if constituencynumber == 35113
replace reelected = 1 if constituencynumber == 35114
replace reelected = 1 if constituencynumber == 35115
replace reelected = 0 if constituencynumber == 35116
replace reelected = 1 if constituencynumber == 35117
replace reelected = 1 if constituencynumber == 35118
replace reelected = 1 if constituencynumber == 35119
replace reelected = 1 if constituencynumber == 35120
replace reelected = 1 if constituencynumber == 35121
replace reelected = 1 if constituencynumber == 46001
replace reelected = 0 if constituencynumber == 46002
replace reelected = 1 if constituencynumber == 46003
replace reelected = 1 if constituencynumber == 46004
replace reelected = 1 if constituencynumber == 46005
replace reelected = 0 if constituencynumber == 46006
replace reelected = 1 if constituencynumber == 46007
replace reelected = 1 if constituencynumber == 46008
replace reelected = 1 if constituencynumber == 46009
replace reelected = 1 if constituencynumber == 46010
replace reelected = 0 if constituencynumber == 46011
replace reelected = 1 if constituencynumber == 46012
replace reelected = 1 if constituencynumber == 46013
replace reelected = 1 if constituencynumber == 46014
replace reelected = 1 if constituencynumber == 47001
replace reelected = 1 if constituencynumber == 47002
replace reelected = 0 if constituencynumber == 47003
replace reelected = 1 if constituencynumber == 47004
replace reelected = 1 if constituencynumber == 47005
replace reelected = 1 if constituencynumber == 47006
replace reelected = 0 if constituencynumber == 47007
replace reelected = 1 if constituencynumber == 47008
replace reelected = 0 if constituencynumber == 47009
replace reelected = 1 if constituencynumber == 47010
replace reelected = 1 if constituencynumber == 47011
replace reelected = 0 if constituencynumber == 47012
replace reelected = 1 if constituencynumber == 47013
replace reelected = 1 if constituencynumber == 47014
replace reelected = 1 if constituencynumber == 48001
replace reelected = 1 if constituencynumber == 48002
replace reelected = 1 if constituencynumber == 48003
replace reelected = 0 if constituencynumber == 48004
replace reelected = 1 if constituencynumber == 48005
replace reelected = 1 if constituencynumber == 48006
replace reelected = 1 if constituencynumber == 48007
replace reelected = 1 if constituencynumber == 48008
replace reelected = 1 if constituencynumber == 48009
replace reelected = 1 if constituencynumber == 48010
replace reelected = 1 if constituencynumber == 48011
replace reelected = 1 if constituencynumber == 48012
replace reelected = 0 if constituencynumber == 48013
replace reelected = 0 if constituencynumber == 48014
replace reelected = 1 if constituencynumber == 48015
replace reelected = 1 if constituencynumber == 48016
replace reelected = 0 if constituencynumber == 48017
replace reelected = 1 if constituencynumber == 48018
replace reelected = 1 if constituencynumber == 48019
replace reelected = 1 if constituencynumber == 48020
replace reelected = 1 if constituencynumber == 48021
replace reelected = 1 if constituencynumber == 48022
replace reelected = 1 if constituencynumber == 48023
replace reelected = 1 if constituencynumber == 48024
replace reelected = 1 if constituencynumber == 48025
replace reelected = 1 if constituencynumber == 48026
replace reelected = 1 if constituencynumber == 48027
replace reelected = 1 if constituencynumber == 48028
replace reelected = 1 if constituencynumber == 48029
replace reelected = 1 if constituencynumber == 48030
replace reelected = 1 if constituencynumber == 48031
replace reelected = 1 if constituencynumber == 48032
replace reelected = 1 if constituencynumber == 48033
replace reelected = 1 if constituencynumber == 48034
replace reelected = 1 if constituencynumber == 59001
replace reelected = 1 if constituencynumber == 59002
replace reelected = 1 if constituencynumber == 59003
replace reelected = 1 if constituencynumber == 59004
replace reelected = 1 if constituencynumber == 59005
replace reelected = 1 if constituencynumber == 59006
replace reelected = 0 if constituencynumber == 59007
replace reelected = 1 if constituencynumber == 59008
replace reelected = 1 if constituencynumber == 59009
replace reelected = 1 if constituencynumber == 59010
replace reelected = 1 if constituencynumber == 59011
replace reelected = 1 if constituencynumber == 59012
replace reelected = 1 if constituencynumber == 59013
replace reelected = 0 if constituencynumber == 59014
replace reelected = 0 if constituencynumber == 59015
replace reelected = 1 if constituencynumber == 59016
replace reelected = 0 if constituencynumber == 59017
replace reelected = 0 if constituencynumber == 59018
replace reelected = 1 if constituencynumber == 59019
replace reelected = 1 if constituencynumber == 59020
replace reelected = 1 if constituencynumber == 59021
replace reelected = 0 if constituencynumber == 59022
replace reelected = 0 if constituencynumber == 59023
replace reelected = 1 if constituencynumber == 59024
replace reelected = 1 if constituencynumber == 59025
replace reelected = 1 if constituencynumber == 59026
replace reelected = 0 if constituencynumber == 59027
replace reelected = 1 if constituencynumber == 59028
replace reelected = 1 if constituencynumber == 59029
replace reelected = 1 if constituencynumber == 59030
replace reelected = 0 if constituencynumber == 59031
replace reelected = 1 if constituencynumber == 59032
replace reelected = 1 if constituencynumber == 59033
replace reelected = 1 if constituencynumber == 59034
replace reelected = 1 if constituencynumber == 59035
replace reelected = 0 if constituencynumber == 59036
replace reelected = 1 if constituencynumber == 59037
replace reelected = 1 if constituencynumber == 59038
replace reelected = 1 if constituencynumber == 59039
replace reelected = 1 if constituencynumber == 59040
replace reelected = 1 if constituencynumber == 59041
replace reelected = 1 if constituencynumber == 59042
replace reelected = 1 if constituencynumber == 60001
replace reelected = 1 if constituencynumber == 61001
replace reelected = 0 if constituencynumber == 62001


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen surveydate = dofc(cps19_EndDate)

format %tdMon_DD,_CCYY surveydate

gen edate = 20191021

gen electiondate=date(string(edate,"%8.0f"),"YMD") + 1

format %tdMon_DD,_CCYY electiondate

gen time = (electiondate - surveydate) 

replace time = . if time < 0

* Province

gen province = cps19_province

recode province 14=2 15=1 16=4 17=7 18=10 19=11 20=8 21=13 22=5 23=9 24=6 25=3 26=12

label define province 1 "British Columbia" 2 "Alberta" 3 "Saskatchewan" 4 "Manitoba" 5 "Ontario" 6 "Quebec" 7 "New Brunswick" 8 "Nova Scotia" 9 "Prince Edward Island" 10 "Newfoundland and Labrador" 11 "Northwest Territories" 12 "Yukon" 13 "Nunavut"
label values province province

* Language

encode cps19_Q_Language, gen(language)

recode lang 1=0 2=1

label define lang 0 "English" 1 "French"
label values language lang

* Gender

gen male = cps19_gender

recode male 2=0 3=.

label define gender 0 "Female" 1 "Male"
label values male gender

* Age

gen age = 2019 - (cps19_yob + 1919)

replace age = . if age < 17

* Age 55 and over

gen age55 = 1 if age >= 55
replace age55 = 0 if age < 55 & age!=.

label define age55 0 "Less than 55" 1 "55 and over"
label values age55 age55

* Education

gen education = cps19_education

recode education 12=.

recode education 1=901 2=902 3=903 4=904 5=905 6=906 7=907 8=908 9=909 10=910 11=911

label define education 901 "No schooling" 902 "Some elementary school" 903 "Completed elementary school" 904 "Some secondary/high school" 905 "Completed secondary/high school" 906 "Some technical, community college, CEGEP, College Classique" 907 "Completed technical, community college, CEGEP, College Classique" 908 "Some university" 909 "Bachelor's degree" 910 "Master's degree" 911 "Professional degree or doctorate" 
label values education education

* University degree

gen univ = 1 if education >= 909
replace univ = 0 if education < 909 & education!=.

label define univ 0 "No university degree" 1 "University degree"
label values univ univ

* Postgraduate degree

gen postgrad = 1 if education == 910
replace postgrad = 0 if education < 910 & education!=.

label define postgrad 0 "No postgraduate degree" 1 "Postgraduate degree"
label values postgrad postgrad

* Postsecondary degree

gen postsec = 1 if education >= 907
replace postsec = 0 if education < 907 & education!=.

label define postsec 0 "No postsecondary" 1 "Postsecondary"
label values postsec postsec

* Immigrant

gen immigrant = cps19_bornin_canada

recode immigrant 1=0 2=1 3=.

label define immigrant 0 "Born in Canada" 1 "Immigrant"
label values immigrant immigrant

* Household income

gen income_n = cps19_income_number

* Household income (categories)

gen income = cps19_income_cat

recode income 1=881 2=882 3=883 4=884 5=885 6=886 7=887 8=888 9=.

label define income 881 "No income" 882 "$1-$30,000" 883 "$30,001-$60,000" 884 "$60,001-$90,000" 885 "$90,001-$110,000" 886 "$110,001-$150,000" 887 "$150,001-$200,000" 888 "More than $200,000"
label values income income

gen income_c = income_n

recode income_c 0=881 1/30000=882 30001/60000=883 60001/90000=884 90001/110000=885 110001/150000=886 150001/200000=887
replace income_c = 888 if income_n > 200000

replace income = income_c if income==.

* Household income (quintile)

xtile income5 = income, nq(5)

* Highest income categories

gen highinc = 1 if income5 == 5
replace highinc = 0 if income5 < 5 & income!=.

label define highinc 0 "Others" 1 "Highest incomes"
label values highinc highinc

* Interest for election

gen interest = cps19_interest_elxn

* High political interest

gen highint = 1 if interest > 7
replace highint = 0 if interest < 8

* Name of Governor General of Canada

gen govgen = cps19_govgen_name_right

recode govgen 2=0 3=0 96=0 97=0 98=0 99=.5

* Name of Finance Minister of Canada

gen finmin = cps19_finmin_name_right

recode finmin 2=0 3=0 96=0 97=0 98=0 99=.5

* Name of Premier

gen provpremier = cps19_premier_name_right

recode provpremier 2=0 3=0 96=0 97=0 98=0 99=.5

* Name of President of Russia

gen presrus = cps19_presrus_name_right

recode presrus 2=0 3=0 96=0 97=0 98=0 99=.5

* Political knowledge

gen knowledge = govgen + finmin + provpremier + presrus

* Vote choice

gen voteint = cps19_votechoice

recode voteint 9=.

gen votecould = cps19_votechoice_pr

recode votecould 9=.

gen ifvote = cps19_vote_unlikely

recode ifvote 9=.

gen ifvotecould = cps19_vote_unlike_pr

recode ifvotecould 9=.

gen voteadvance =  cps19_v_advance

recode voteadvance 9=.

gen votelean = cps19_vote_lean

recode votelean 8=0 9=.

gen voteleancould = cps19_vote_lean_pr

recode voteleancould 8=0 9=.

egen vote_choice = rowmax(voteint votecould ifvote ifvotecould voteadvance votelean voteleancould)

recode vote_choice 1=2 2=1 7=88 // Recode to match other datasets

label define voteint 0 "None/Will not vote" 1 "CPC" 2 "LPC" 3 "NDP" 4 "BQ" 5 "GPC" 6 "PPC" 88 "Other"
label values vote_choice voteint

* Vote status (district)

gen vote_district = 1 if vote_choice == district_outcome
replace vote_district = 0 if vote_choice!=district_outcome & vote_choice!=. & vote_choice!=0 

label define winner 0 "Voted for loser" 1 "Voted for winner"
label values vote_district winner

* Vote status (national)

gen vote_whole = 1 if vote_choice == 2
replace vote_whole = 0 if vote_choice!=2 & vote_choice!=. 

label values vote_whole winner

* Party identification

gen pid = cps19_fed_id

recode pid 8=0 1=2 2=1 7=88

label define pid 0 "No PID" 1 "Conservative" 2 "Liberal" 3 "NDP" 4 "Bloc" 5 "Green" 6 "People's Party" 88 "Other" 9 "Don't know/Prefer not to answer"
label values pid pid

* Party identification strength

gen pid_strength = cps19_fed_id_str

recode pid_strength 1=3 3=1 4=.

label define strength 1 "Not very strongly" 2 "Fairly strongly" 3 "Very strongly"
label values pid_strength strength

* Party identification status (district)

gen pidstatus_district = .

replace pidstatus_district = 2 if pid == 0
replace pidstatus_district = 3 if pid == district_outcome & pid!=.
replace pidstatus_district = 1 if pid!=district_outcome & pid!=. & pidstatus_district!=2
replace pidstatus_district = 1 if pid == 2 & district_outcome!=2
replace pidstatus_district = 3 if pid == 2 & district_outcome == 2

replace pidstatus_district = . if pid == 9

label define pidstatus 1 "Loser PID" 2 "No PID" 3 "Winner PID" 
label values pidstatus_district pidstatus

* Party identification status (national)

gen pidstatus_whole = .

replace pidstatus_whole = 2 if pid == 0
replace pidstatus_whole = 3 if pid == 1
replace pidstatus_whole = 1 if pid!=1 & pid!=. & pidstatus_whole!=2
replace pidstatus_whole = 1 if pid == 2

replace pidstatus_whole = . if pid == 9

label values pidstatus_whole pidstatus

* Party identification scale (district)

gen pidscale_district = .

replace pidscale_district = 1 if pidstatus_district == 1 & pid_strength == 3
replace pidscale_district = 2 if pidstatus_district == 1 & pid_strength == 2
replace pidscale_district = 3 if pidstatus_district == 1 & pid_strength == 1
replace pidscale_district = 4 if pidstatus_district == 2
replace pidscale_district = 5 if pidstatus_district == 3 & pid_strength == 1
replace pidscale_district = 6 if pidstatus_district == 3 & pid_strength == 2
replace pidscale_district = 7 if pidstatus_district == 3 & pid_strength == 3

label define pidscale 1 "Strong loser PID" 2 "Moderate loser PID" 3 "Weak loser PID" 4 "No PID" 5 "Weak winner PID" 6 "Moderate winner PID" 7 "Strong winner PID"
label values pidscale_district pidscale

* Party identification scale (national)

gen pidscale_whole = .

replace pidscale_whole = 1 if pidstatus_whole == 1 & pid_strength == 3
replace pidscale_whole = 2 if pidstatus_whole == 1 & pid_strength == 2
replace pidscale_whole = 3 if pidstatus_whole == 1 & pid_strength == 1
replace pidscale_whole = 4 if pidstatus_whole == 2
replace pidscale_whole = 5 if pidstatus_whole == 3 & pid_strength == 1
replace pidscale_whole = 6 if pidstatus_whole == 3 & pid_strength == 2
replace pidscale_whole = 7 if pidstatus_whole == 3 & pid_strength == 3

label values pidscale_whole pidscale

* Relative feeling toward party of choice

gen plc1 = cps19_party_rating_24
gen plc2 = cps19_party_rating_25
gen plc3 = cps19_party_rating_26
gen plc4 = cps19_party_rating_27
gen plc5 = cps19_party_rating_28

rowsort plc*, gen(lib1-lib5) descend

gen plc0 = cps19_party_rating_23

gen cpc1 = cps19_party_rating_23
gen cpc2 = cps19_party_rating_25
gen cpc3 = cps19_party_rating_26
gen cpc4 = cps19_party_rating_27
gen cpc5 = cps19_party_rating_28

rowsort cpc*, gen(con1-con5) descend

gen cpc0 = cps19_party_rating_24

gen ndp1 = cps19_party_rating_23
gen ndp2 = cps19_party_rating_24
gen ndp3 = cps19_party_rating_26
gen ndp4 = cps19_party_rating_27
gen ndp5 = cps19_party_rating_28

rowsort ndp*, gen(new1-new5) descend

gen ndp0 = cps19_party_rating_25

gen bq1 = cps19_party_rating_23
gen bq2 = cps19_party_rating_24
gen bq3 = cps19_party_rating_25
gen bq4 = cps19_party_rating_27
gen bq5 = cps19_party_rating_28

rowsort bq*, gen(bloc1-bloc5) descend

gen bq0 = cps19_party_rating_26

gen gpc1 = cps19_party_rating_23
gen gpc2 = cps19_party_rating_24
gen gpc3 = cps19_party_rating_25
gen gpc4 = cps19_party_rating_26
gen gpc5 = cps19_party_rating_28

rowsort gpc*, gen(grn1-grn5) descend

gen gpc0 = cps19_party_rating_27

gen ppc1 = cps19_party_rating_23
gen ppc2 = cps19_party_rating_24
gen ppc3 = cps19_party_rating_25
gen ppc4 = cps19_party_rating_26
gen ppc5 = cps19_party_rating_27

rowsort ppc*, gen(pop1-pop5) descend

gen ppc0 = cps19_party_rating_28

gen first_party = .

replace first_party = plc0 if vote_choice == 2
replace first_party = cpc0 if vote_choice == 1
replace first_party = ndp0 if vote_choice == 3
replace first_party = bq0 if vote_choice == 4
replace first_party = gpc0 if vote_choice == 5
replace first_party = ppc0 if vote_choice == 6

gen relative = .

replace relative = plc0 - plc1 if vote_choice == 2 
replace relative = cpc0 - cpc1 if vote_choice == 1 
replace relative = ndp0 - ndp1 if vote_choice == 3 
replace relative = bq0 - bq1 if vote_choice == 4
replace relative = gpc0 - gpc1 if vote_choice == 5 
replace relative = ppc0 - ppc1 if vote_choice == 6 


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

gen lpcchance_district = cps19_win_local_1
gen cpcchance_district = cps19_win_local_2
gen ndpchance_district = cps19_win_local_3
gen bqchance_district = cps19_win_local_4
gen gpcchance_district = cps19_win_local_5
gen ppcchance_district = cps19_win_local_6
   
* Generate variable identifying incomplete answers (BQ in Quebec only)

gen complete_district_qc = 1 if cpcchance_district!=. & ndpchance_district!=. & lpcchance_district!=. & bqchance_district!=. & gpcchance_district!=. & province == 6 // If the respondent gave an expectation score to each party candidate, treat his/her answers as complete (Quebec respondents)

gen complete_district_roc = 1 if cpcchance_district!=. & ndpchance_district!=. & lpcchance_district!=. & gpcchance_district!=. & province!=6 // If the respondent gave an expectation score to each party candidate, treat his/her answers as complete (ROC respondents)

gen complete_district = max(complete_district_qc, complete_district_roc)

* Generate a value of 1 if a party candidate received the highest expectation score

gen lpc_district_qc = 1 if lpcchance_district >= cpcchance_district & lpcchance_district >= ndpchance_district & lpcchance_district >= bqchance_district & lpcchance_district >= gpcchance_district & lpcchance_district >= ppcchance_district & complete_district_qc == 1 // The LPC candidate received the highest expectation score given by the respondent 
gen cpc_district_qc = 1 if cpcchance_district >= lpcchance_district & cpcchance_district >= ndpchance_district & cpcchance_district >= bqchance_district & cpcchance_district >= gpcchance_district & cpcchance_district >= ppcchance_district & complete_district_qc == 1 // The CPC candidate received the highest expectation score given by the respondent 
gen ndp_district_qc = 1 if ndpchance_district >= lpcchance_district & ndpchance_district >= cpcchance_district & ndpchance_district >= bqchance_district & ndpchance_district >= gpcchance_district & ndpchance_district >= ppcchance_district & complete_district_qc == 1 // The NDP candidate received the highest expectation score given by the respondent 
gen bq_district_qc = 1 if bqchance_district >= lpcchance_district & bqchance_district >= cpcchance_district & bqchance_district >= ndpchance_district & bqchance_district >= gpcchance_district & bqchance_district >= ppcchance_district & complete_district_qc == 1 // The BQ candidate received the highest expectation score given by the respondent 
gen gpc_district_qc = 1 if gpcchance_district >= lpcchance_district & gpcchance_district >= cpcchance_district & gpcchance_district >= ndpchance_district & gpcchance_district >= bqchance_district & gpcchance_district >= ppcchance_district & complete_district_qc == 1 // The GPC candidate received the highest expectation score given by the respondent 
gen ppc_district_qc = 1 if ppcchance_district >= lpcchance_district & ppcchance_district >= cpcchance_district & ppcchance_district >= ndpchance_district & ppcchance_district >= bqchance_district & ppcchance_district >= gpcchance_district & complete_district_qc == 1 // The PPC candidate received the highest expectation score given by the respondent 
gen oth_district_qc = 1 if lpcchance_district == 0 & cpcchance_district == 0 & ndpchance_district == 0 & bqchance_district == 0 & gpcchance_district == 0 & ppcchance_district == 0 & complete_district_qc == 1 // Another candidate received the highest expectation score given by the respondent (if a respondent gave a score of 0 to every party candidate, it is assumed that the respondent believes some other party has the highest chance of winning in the district)

gen lpc_district_roc = 1 if lpcchance_district >= cpcchance_district & lpcchance_district >= ndpchance_district & lpcchance_district >= gpcchance_district & lpcchance_district >= ppcchance_district & complete_district_roc == 1 // The LPC candidate received the highest expectation score given by the respondent 
gen cpc_district_roc = 1 if cpcchance_district >= lpcchance_district & cpcchance_district >= ndpchance_district & cpcchance_district >= gpcchance_district & cpcchance_district >= ppcchance_district & complete_district_roc == 1 // The CPC candidate received the highest expectation score given by the respondent 
gen ndp_district_roc = 1 if ndpchance_district >= lpcchance_district & ndpchance_district >= cpcchance_district & ndpchance_district >= gpcchance_district & ndpchance_district >= ppcchance_district & complete_district_roc == 1 // The NDP candidate received the highest expectation score given by the respondent 
gen gpc_district_roc = 1 if gpcchance_district >= lpcchance_district & gpcchance_district >= cpcchance_district & gpcchance_district >= ndpchance_district & gpcchance_district >= ppcchance_district & complete_district_roc == 1 // The GPC candidate received the highest expectation score given by the respondent 
gen ppc_district_roc = 1 if ppcchance_district >= lpcchance_district & ppcchance_district >= cpcchance_district & ppcchance_district >= ndpchance_district & ppcchance_district >= gpcchance_district & complete_district_roc == 1 // The PPC candidate received the highest expectation score given by the respondent 
gen oth_district_roc = 1 if lpcchance_district == 0 & cpcchance_district == 0 & ndpchance_district == 0 & gpcchance_district == 0 & ppcchance_district == 0 & complete_district_roc == 1 // Another candidate received the highest expectation score given by the respondent (if a respondent gave a score of 0 to every party candidate, it is assumed that the respondent believes some other party has the highest chance of winning in the district)

gen cpc_district = max(cpc_district_qc, cpc_district_roc) if complete_district == 1
gen ndp_district = max(ndp_district_qc, ndp_district_roc) if complete_district == 1
gen lpc_district = max(lpc_district_qc, lpc_district_roc) if complete_district == 1
gen bq_district = bq_district_qc if complete_district == 1
gen gpc_district = max(gpc_district_qc, gpc_district_roc) if complete_district == 1
gen ppc_district = max(ppc_district_qc, ppc_district_roc) if complete_district == 1
gen oth_district = max(oth_district_qc, oth_district_roc) if complete_district == 1 // Merging answers from Quebec respondents and ROC respondents

replace cpc_district = . if oth_district == 1 & complete_district == 1
replace ndp_district = . if oth_district == 1 & complete_district == 1
replace lpc_district = . if oth_district == 1 & complete_district == 1
replace bq_district = . if oth_district == 1 & complete_district == 1
replace gpc_district = . if oth_district == 1 & complete_district == 1 
replace ppc_district = . if oth_district == 1 & complete_district == 1 // This ensures respondents who gave a score of 0 to every party candidate are not treated as providing ambiguous forecasts

* Generate variable for ambiguous forecasts

gen ambiguous_district = .

replace ambiguous_district = 99 if lpc_district == cpc_district & lpc_district!=. & complete_district == 1 | lpc_district == ndp_district & lpc_district!=. & complete_district == 1 | lpc_district == bq_district & lpc_district!=. & complete_district == 1 | lpc_district == gpc_district & lpc_district!=. & complete_district == 1 | lpc_district == ppc_district & lpc_district!=. & complete_district == 1  // The respondent gave his/her highest expectation score to the LPC candidate and at least one other candidate
replace ambiguous_district = 99 if cpc_district == lpc_district & cpc_district!=. & complete_district == 1 | cpc_district == ndp_district & cpc_district!=. & complete_district == 1 | cpc_district == bq_district & cpc_district!=. & complete_district == 1 | cpc_district == gpc_district & cpc_district!=. & complete_district == 1 | cpc_district == ppc_district & cpc_district!=. & complete_district == 1  // The respondent gave his/her highest expectation score to the CPC candidate and at least one other candidate
replace ambiguous_district = 99 if ndp_district == lpc_district & ndp_district!=. & complete_district == 1 | ndp_district == cpc_district & ndp_district!=. & complete_district == 1 | ndp_district == bq_district & ndp_district!=. & complete_district == 1 | ndp_district == gpc_district & ndp_district!=. & complete_district == 1 | ndp_district == ppc_district & ndp_district!=. & complete_district == 1  // The respondent gave his/her highest expectation score to the NDP candidate and at least one other candidate
replace ambiguous_district = 99 if bq_district == lpc_district & bq_district!=. & complete_district == 1 | bq_district == cpc_district & bq_district!=. & complete_district == 1 | bq_district == ndp_district & bq_district!=. & complete_district == 1 | bq_district == gpc_district & bq_district!=. & complete_district == 1 | bq_district == ppc_district & bq_district!=. & complete_district == 1  // The respondent gave his/her highest expectation score to the BQ candidate and at least one other candidate
replace ambiguous_district = 99 if gpc_district == lpc_district & gpc_district!=. & complete_district == 1 | gpc_district == cpc_district & gpc_district!=. & complete_district == 1 | gpc_district == ndp_district & gpc_district!=. & complete_district == 1 | gpc_district == bq_district & gpc_district!=. & complete_district == 1 | gpc_district == ppc_district & gpc_district!=. & complete_district == 1  // The respondent gave his/her highest expectation score to the GPC candidate and at least one other candidate
replace ambiguous_district = 99 if ppc_district == lpc_district & ppc_district!=. & complete_district == 1 | ppc_district == cpc_district & ppc_district!=. & complete_district == 1 | ppc_district == ndp_district & ppc_district!=. & complete_district == 1 | ppc_district == bq_district & ppc_district!=. & complete_district == 1 | ppc_district == gpc_district & ppc_district!=. & complete_district == 1  // The respondent gave his/her highest expectation score to the PPC candidate and at least one other candidate

gen amb_district = 1 if ambiguous_district == 99 & complete_district == 1

* Generate a value of 0 for lower expectations

replace lpc_district = 0 if lpc_district!=1 & complete_district == 1 // The LPC candidate did not receive the highest expectation score given by the respondent 
replace cpc_district = 0 if cpc_district!=1 & complete_district == 1 // The CPC candidate did not receive the highest expectation score given by the respondent 
replace ndp_district = 0 if ndp_district!=1 & complete_district == 1 // The NDP candidate did not receive the highest expectation score given by the respondent 
replace bq_district = 0 if bq_district!=1 & complete_district == 1 // The BQ candidate did not receive the highest expectation score given by the respondent 
replace gpc_district = 0 if gpc_district!=1 & complete_district == 1 // The GPC candidate did not receive the highest expectation score given by the respondent 
replace ppc_district = 0 if ppc_district!=1 & complete_district == 1 // The PPC candidate did not receive the highest expectation score given by the respondent 
replace oth_district = 0 if oth_district!=1 & complete_district == 1 // Another candidate did not receive the highest expectation score given by the respondent 

* Forecasts (1 = CPC, 2 = LPC, 3 = NDP, 4 = BQ, 5 = GPC, 6 = PPC, 88 = other, 99 = ambiguous)

gen forecast_district = .

replace forecast_district = 2 if lpc_district == 1 & complete_district == 1 // The LPC candidate is the predicted winner
replace forecast_district = 1 if cpc_district == 1 & complete_district == 1 // The CPC candidate is the predicted winner
replace forecast_district = 3 if ndp_district == 1 & complete_district == 1 // The NDP candidate is the predicted winner
replace forecast_district = 4 if bq_district == 1 & complete_district == 1 // The BQ candidate is the predicted winner
replace forecast_district = 5 if gpc_district == 1 & complete_district == 1 // The GPC candidate is the predicted winner
replace forecast_district = 6 if ppc_district == 1 & complete_district == 1 // The PPC candidate is the predicted winner
replace forecast_district = 88 if oth_district == 1 & complete_district == 1 // Another candidate is the predicted winner
replace forecast_district = 99 if ambiguous_district == 99 & complete_district == 1 // Ambiguous forecast

* Identify correct and ambiguous forecasts

gen correct_district = 0 if forecast_district!=district_outcome & forecast_district!=99 & complete_district == 1 // The forecast is incorrect

replace correct_district = 1 if forecast_district == district_outcome & forecast_district!=99 & complete_district == 1 // The forecast is correct

replace correct_district = 0 if forecast_district == 99 & lpc_district!=1 & district_outcome == 2 & complete_district == 1 // The ambiguous forecast is incorrect since the LPC candidate is not among the candidates who received the highest score
replace correct_district = 0 if forecast_district == 99 & cpc_district!=1 & district_outcome == 1 & complete_district == 1 // The ambiguous forecast is incorrect since the CPC candidate is not among the candidates who received the highest score
replace correct_district = 0 if forecast_district == 99 & ndp_district!=1 & district_outcome == 3 & complete_district == 1 // The ambiguous forecast is incorrect since the NDP candidate is not among the candidates who received the highest score
replace correct_district = 0 if forecast_district == 99 & bq_district!=1 & district_outcome == 4 & complete_district == 1 // The ambiguous forecast is incorrect since the BQ candidate is not among the candidates who received the highest score
replace correct_district = 0 if forecast_district == 99 & gpc_district!=1 & district_outcome == 5 & complete_district == 1 // The ambiguous forecast is incorrect since the GPC candidate is not among the candidates who received the highest score
replace correct_district = 0 if forecast_district == 99 & ppc_district!=1 & district_outcome == 6 & complete_district == 1 // The ambiguous forecast is incorrect since the PPC candidate is not among the candidates who received the highest score

replace correct_district = 99 if forecast_district == 99 & lpc_district == 1 & district_outcome == 2 & complete_district == 1 // The forecast is truly ambiguous since the LPC candidate is among the candidates who received the highest score
replace correct_district = 99 if forecast_district == 99 & cpc_district == 1 & district_outcome == 1 & complete_district == 1 // The forecast is truly ambiguous since the CPC candidate is among the candidates who received the highest score
replace correct_district = 99 if forecast_district == 99 & ndp_district == 1 & district_outcome == 3 & complete_district == 1 // The forecast is truly ambiguous since the NDP candidate is among the candidates who received the highest score
replace correct_district = 99 if forecast_district == 99 & bq_district == 1 & district_outcome == 4 & complete_district == 1 // The forecast is truly ambiguous since the BQ candidate is among the candidates who received the highest score
replace correct_district = 99 if forecast_district == 99 & gpc_district == 1 & district_outcome == 5 & complete_district == 1 // The forecast is truly ambiguous since the GPC candidate is among the candidates who received the highest score
replace correct_district = 99 if forecast_district == 99 & ppc_district == 1 & district_outcome == 6 & complete_district == 1 // The forecast is truly ambiguous since the PPC candidate is among the candidates who received the highest score

* Generate dichotomous variable

gen correct_district_d = correct_district

replace correct_district_d = 0 if correct_district_d == 99 // Ambiguous forecasts are treated as incorrect
replace correct_district_d = 1 if correct_district_d == 1


*********************************
** NATIONAL-LEVEL EXPECTATIONS ** 
*********************************

* Generate variable identifying incomplete answers

gen lpcchance_whole = cps19_most_seats_1
gen cpcchance_whole = cps19_most_seats_2
gen ndpchance_whole = cps19_most_seats_3
gen bqchance_whole = cps19_most_seats_4
gen gpcchance_whole = cps19_most_seats_5
gen ppcchance_whole = cps19_most_seats_6

gen complete_whole = 1 if lpcchance_whole!=. & cpcchance_whole!=. & ndpchance_whole!=. & bqchance_whole!=. & gpcchance_whole!=. & ppcchance_whole!=.

* Generate value of 1 if the expectation for one party is higher or equal to the expectations for at least one other party

gen lpc_whole = 1 if lpcchance_whole >= cpcchance_whole & lpcchance_whole >= ndpchance_whole & lpcchance_whole >= bqchance_whole & lpcchance_whole >= gpcchance_whole & lpcchance_whole >= ppcchance_whole & complete_whole == 1

gen cpc_whole = 1 if cpcchance_whole >= lpcchance_whole & cpcchance_whole >= ndpchance_whole & cpcchance_whole >= bqchance_whole & cpcchance_whole >= gpcchance_whole & cpcchance_whole >= ppcchance_whole & complete_whole == 1

gen ndp_whole = 1 if ndpchance_whole >= lpcchance_whole & ndpchance_whole >= cpcchance_whole & ndpchance_whole >= bqchance_whole & ndpchance_whole >= gpcchance_whole & ndpchance_whole >= ppcchance_whole & complete_whole == 1

gen bq_whole = 1 if bqchance_whole >= lpcchance_whole & bqchance_whole >= cpcchance_whole & bqchance_whole >= ndpchance_whole & bqchance_whole >= gpcchance_whole & bqchance_whole >= ppcchance_whole & complete_whole == 1

gen gpc_whole = 1 if gpcchance_whole >= lpcchance_whole & gpcchance_whole >= cpcchance_whole & gpcchance_whole >= ndpchance_whole & gpcchance_whole >= bqchance_whole & gpcchance_whole >= ppcchance_whole & complete_whole == 1

gen ppc_whole = 1 if ppcchance_whole >= lpcchance_whole & ppcchance_whole >= cpcchance_whole & ppcchance_whole >= ndpchance_whole & ppcchance_whole >= bqchance_whole & ppcchance_whole >= gpcchance_whole & complete_whole == 1

gen oth_whole = 1 if lpcchance_whole == 0 & cpcchance_whole == 0 & ndpchance_whole == 0 & bqchance_whole == 0 & gpcchance_whole == 0 & ppcchance_whole == 0 & complete_whole == 1

replace lpc_whole = . if oth_whole == 1 & complete_whole == 1
replace cpc_whole = . if oth_whole == 1 & complete_whole == 1
replace ndp_whole = . if oth_whole == 1 & complete_whole == 1
replace bq_whole = . if oth_whole == 1 & complete_whole == 1
replace gpc_whole = . if oth_whole == 1 & complete_whole == 1
replace ppc_whole = . if oth_whole == 1 & complete_whole == 1

* Generate variable for ambiguous forecasts

gen ambiguous_whole = .

replace ambiguous_whole = 99 if lpc_whole == cpc_whole & lpc_whole!=. | lpc_whole == ndp_whole & lpc_whole!=. | lpc_whole == bq_whole & lpc_whole!=. | lpc_whole == gpc_whole & lpc_whole!=. | lpc_whole == ppc_whole & lpc_whole!=.

replace ambiguous_whole = 99 if cpc_whole == lpc_whole & cpc_whole!=. | cpc_whole == ndp_whole & cpc_whole!=. | cpc_whole == bq_whole & cpc_whole!=. | cpc_whole == gpc_whole & cpc_whole!=. | cpc_whole == ppc_whole & cpc_whole!=.

replace ambiguous_whole = 99 if ndp_whole == lpc_whole & ndp_whole!=. | ndp_whole == cpc_whole & ndp_whole!=. | ndp_whole == bq_whole & ndp_whole!=. | ndp_whole == gpc_whole & ndp_whole!=. | ndp_whole == ppc_whole & ndp_whole!=.

replace ambiguous_whole = 99 if bq_whole == lpc_whole & bq_whole!=. | bq_whole == cpc_whole & bq_whole!=. | bq_whole == ndp_whole & bq_whole!=. | bq_whole == gpc_whole & bq_whole!=. | bq_whole == ppc_whole & bq_whole!=.

replace ambiguous_whole = 99 if gpc_whole == lpc_whole & gpc_whole!=. | gpc_whole == cpc_whole & gpc_whole!=. | gpc_whole == ndp_whole & gpc_whole!=. | gpc_whole == bq_whole & gpc_whole!=. | gpc_whole == ppc_whole & gpc_whole!=.

replace ambiguous_whole = 99 if ppc_whole == lpc_whole & ppc_whole!=. | ppc_whole == cpc_whole & ppc_whole!=. | ppc_whole == ndp_whole & ppc_whole!=. | ppc_whole == bq_whole & ppc_whole!=. | ppc_whole == gpc_whole & ppc_whole!=.

* Generate value of 0 for lower expectations

replace lpc_whole = 0 if lpc_whole!=1 & lpc_whole!=. & complete_whole == 1
replace cpc_whole = 0 if cpc_whole!=1 & cpc_whole!=. & complete_whole == 1
replace ndp_whole = 0 if ndp_whole!=1 & ndp_whole!=. & complete_whole == 1
replace bq_whole = 0 if bq_whole!=1 & bq_whole!=. & complete_whole == 1
replace gpc_whole = 0 if gpc_whole!=1 & gpc_whole!=. & complete_whole == 1
replace ppc_whole = 0 if ppc_whole!=1 & ppc_whole!=. & complete_whole == 1
replace oth_whole = 0 if oth_whole!=1 & oth_whole!=. & complete_whole == 1

* Forecasts (1 = CPC, 2 = LPC, 3 = NDP, 4 = BQ, 5 = GPC, 6 = PPC, 88 = other, 99 = ambiguous)

gen forecast_whole = .

replace forecast_whole = 2 if lpc_whole == 1 & complete_whole == 1
replace forecast_whole = 1 if cpc_whole == 1 & complete_whole == 1
replace forecast_whole = 3 if ndp_whole == 1 & complete_whole == 1
replace forecast_whole = 4 if bq_whole == 1 & complete_whole == 1
replace forecast_whole = 5 if gpc_whole == 1 & complete_whole == 1
replace forecast_whole = 6 if ppc_whole == 1 & complete_whole == 1
replace forecast_whole = 88 if oth_whole == 1 & complete_whole == 1
replace forecast_whole = 99 if ambiguous_whole == 99 & complete_whole == 1

* Identify correct and ambiguous forecasts

gen correct_whole = 0 if forecast_whole!=1 & forecast_whole!=. & complete_whole == 1
replace correct_whole = 1 if forecast_whole == 1 & complete_whole == 1

replace correct_whole = 99 if forecast_whole == 99 & lpc_whole == 1 & complete_whole == 1

* Generate dichotomous variable (treat ambiguous forecasts as incorrect)

gen correct_whole_d = correct_whole

replace correct_whole_d = 0 if correct_whole_d == 99
replace correct_whole_d = 1 if correct_whole_d == 1


*************
** WEIGHTS **
*************

* Sex

gen sex_ca2019_w = male

recode sex_ca2019_w 0=1 1=2

label define sex_ca2019_w 1 "Female" 2 "Male"
label values sex_ca2019_w sex_ca2019_w

* Age

gen age_ca2019_w = age

replace age_ca2019_w = 1 if age_ca2019_w >= 18 & age_ca2019_w <= 19
replace age_ca2019_w = 2 if age_ca2019_w >= 20 & age_ca2019_w <= 24
replace age_ca2019_w = 3 if age_ca2019_w >= 25 & age_ca2019_w <= 29
replace age_ca2019_w = 4 if age_ca2019_w >= 30 & age_ca2019_w <= 34
replace age_ca2019_w = 5 if age_ca2019_w >= 35 & age_ca2019_w <= 39
replace age_ca2019_w = 6 if age_ca2019_w >= 40 & age_ca2019_w <= 44
replace age_ca2019_w = 7 if age_ca2019_w >= 45 & age_ca2019_w <= 49
replace age_ca2019_w = 8 if age_ca2019_w >= 50 & age_ca2019_w <= 54
replace age_ca2019_w = 9 if age_ca2019_w >= 55 & age_ca2019_w <= 59
replace age_ca2019_w = 10 if age_ca2019_w >= 60 & age_ca2019_w <= 64
replace age_ca2019_w = 11 if age_ca2019_w >= 65 & age_ca2019_w <= 69
replace age_ca2019_w = 12 if age_ca2019_w >= 70 & age_ca2019_w <= 74
replace age_ca2019_w = 13 if age_ca2019_w >= 75 & age_ca2019_w <= 79
replace age_ca2019_w = 14 if age_ca2019_w >= 80 & age_ca2019_w <= 84
replace age_ca2019_w = 15 if age_ca2019_w >= 85

label define age_ca2019_w 1 "18-19" 2 "20-24" 3 "25-29" 4 "30-34" 5 "35-39" 6 "40-44" 7 "45-49" 8 "50-54" 9 "55-59" 10 "60-64" 11 "65-69" 12 "70-74" 13 "75-79" 14 "80-84" 15 "85+"
label values age_ca2019_w age_ca2019_w

* Education

gen education_ca2019_w = cps19_education

recode education_ca2019_w 2=1 3=1 4=1 5=2 6=2 7=3 8=3 9=4 10=5 11=5 12=.

label define education_ca2019_w 1 "Below high school" 2 "High school" 3 "Postsecondary below bachelor" 4 "University undergraduate degree" 5 "University graduate degree"
label values education_ca2019_w education_ca2019_w

* Income

gen income_ca2019_w = income

recode income_ca2019_w 881=1 882=1 883=2 884=3 885=4 886=5 887=6 888=7

label define income_ca2019_w 1 "Less than $30,000" 2 "$30,001-$60,000" 3 "$60,001-$90,000" 4 "$90,001-$110,000" 5 "$110,001-$150,000" 6 "$150,001-$200,000" 7 "More than $200,000"
label values income_ca2019_w income_ca2019_w

* Province

gen province_ca2019_w = province

replace province_ca2019_w = 10 if province == 1
replace province_ca2019_w = 9 if province == 2
replace province_ca2019_w = 8 if province == 3
replace province_ca2019_w = 7 if province == 4
replace province_ca2019_w = 6 if province == 5
replace province_ca2019_w = 5 if province == 6
replace province_ca2019_w = 4 if province == 7
replace province_ca2019_w = 3 if province == 8
replace province_ca2019_w = 2 if province == 9
replace province_ca2019_w = 1 if province == 10
replace province_ca2019_w = 11 if province == 11
replace province_ca2019_w = 11 if province == 12
replace province_ca2019_w = 11 if province == 13

label define province_ca2019_w 1 "Newfoundland and Labrador" 2 "Prince Edward Island" 3 "Nova Scotia" 4 "New Brunswick" 5 "Quebec" 6 "Ontario" 7 "Manitoba" 8 "Saskatchewan" 9 "Alberta" 10 "British Columbia" 11 "Northern Canada"
label values province_ca2019_w province_ca2019_w


****************************
** PLURALITY-RANGE VOTING **
****************************

* Generate weighting variables

egen min_ability = min(interest)
egen max_ability = max(interest)

gen ability_w = (interest - min_ability)/(max_ability - min_ability)

* Generate percentage of correct forecasts for each party by district (mean will be 0 for BQ outside Quebec)

gen percent_cpc = 1 if forecast_district == 1 & ability_w!=.
replace percent_cpc = 0 if percent_cpc!=1 & ability_w!=.

gen percent_ndp = 1 if forecast_district == 3 & ability_w!=.
replace percent_ndp = 0 if percent_ndp!=1 & ability_w!=.

gen percent_lpc = 1 if forecast_district == 2 & ability_w!=.
replace percent_lpc = 0 if percent_lpc!=1 & ability_w!=.

gen percent_bq = 1 if forecast_district == 4 & ability_w!=.
replace percent_bq = 0 if percent_bq!=1 & ability_w!=.

gen percent_gpc = 1 if forecast_district == 5 & ability_w!=.
replace percent_gpc = 0 if percent_gpc!=1 & ability_w!=.

gen percent_oth = 1 if forecast_district == 88 & ability_w!=.
replace percent_oth = 0 if percent_oth!=1 & ability_w!=.

gen percent_amb = 1 if forecast_district == 99 & ability_w!=.
replace percent_amb = 0 if percent_amb!=1 & ability_w!=.

egen mean_cpc = mean(percent_cpc), by(constituencynumber)
egen mean_ndp = mean(percent_ndp), by(constituencynumber)
egen mean_lpc = mean(percent_lpc), by(constituencynumber)
egen mean_bq = mean(percent_bq), by(constituencynumber)
egen mean_gpc = mean(percent_gpc), by(constituencynumber)
egen mean_oth = mean(percent_oth), by(constituencynumber)
egen mean_amb = mean(percent_amb), by(constituencynumber)

* Generate weighted forecast for each respondent

gen cpc_w1 = cpc_district*ability_w
gen ndp_w1 = ndp_district*ability_w
gen lpc_w1 = lpc_district*ability_w
gen bq_w1 = bq_district*ability_w
gen gpc_w1 = gpc_district*ability_w
gen oth_w1 = oth_district*ability_w
gen amb_w1 = amb_district*ability_w

* Generate sum of expectations for each party by district

egen cpc_sum_w1 = sum(cpc_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen ndp_sum_w1 = sum(ndp_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen lpc_sum_w1 = sum(lpc_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen bq_sum_w1 = sum(bq_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen gpc_sum_w1 = sum(gpc_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen oth_sum_w1 = sum(oth_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen amb_sum_w1 = sum(amb_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)

* Generate value of 1 if the sum of expectations for one candidate in a district is higher or equal to the sum of expectations for other candidates

gen r_cpc_w1 = 1 if cpc_sum_w1 >= ndp_sum_w1 & cpc_sum_w1 >= lpc_sum_w1 & cpc_sum_w1 >= bq_sum_w1 & cpc_sum_w1 >= gpc_sum_w1 & cpc_sum_w1 >= oth_sum_w1 & cpc_sum_w1 >= amb_sum_w1
gen r_ndp_w1 = 1 if ndp_sum_w1 >= cpc_sum_w1 & ndp_sum_w1 >= lpc_sum_w1 & ndp_sum_w1 >= bq_sum_w1 & ndp_sum_w1 >= gpc_sum_w1 & ndp_sum_w1 >= oth_sum_w1 & ndp_sum_w1 >= amb_sum_w1
gen r_lpc_w1 = 1 if lpc_sum_w1 >= cpc_sum_w1 & lpc_sum_w1 >= ndp_sum_w1 & lpc_sum_w1 >= bq_sum_w1 & lpc_sum_w1 >= gpc_sum_w1 & lpc_sum_w1 >= oth_sum_w1 & lpc_sum_w1 >= amb_sum_w1
gen r_bq_w1 = 1 if bq_sum_w1 >= cpc_sum_w1 & bq_sum_w1 >= ndp_sum_w1 & bq_sum_w1 >= lpc_sum_w1 & bq_sum_w1 >= gpc_sum_w1 & bq_sum_w1 >= oth_sum_w1 & bq_sum_w1 >= amb_sum_w1
gen r_gpc_w1 = 1 if gpc_sum_w1 >= cpc_sum_w1 & gpc_sum_w1 >= ndp_sum_w1 & gpc_sum_w1 >= lpc_sum_w1 & gpc_sum_w1 >= bq_sum_w1 & gpc_sum_w1 >= oth_sum_w1 & gpc_sum_w1 >= amb_sum_w1
gen r_oth_w1 = 1 if oth_sum_w1 >= cpc_sum_w1 & oth_sum_w1 >= ndp_sum_w1 & oth_sum_w1 >= lpc_sum_w1 & oth_sum_w1 >= bq_sum_w1 & oth_sum_w1 >= gpc_sum_w1 & oth_sum_w1 >= amb_sum_w1
gen r_amb_w1 = 1 if amb_sum_w1 >= cpc_sum_w1 & amb_sum_w1 >= ndp_sum_w1 & amb_sum_w1 >= lpc_sum_w1 & amb_sum_w1 >= bq_sum_w1 & amb_sum_w1 >= gpc_sum_w1 & amb_sum_w1 >= oth_sum_w1

* Generate variable for districts with equal sums of expectations for two or more candidates

gen sum_tie_w1 = .

replace sum_tie_w1 = 999 if r_cpc_w1 == r_ndp_w1 & r_cpc_w1!=. | r_cpc_w1 == r_lpc_w1 & r_cpc_w1!=. | r_cpc_w1 == r_bq_w1 & r_cpc_w1!=. | r_cpc_w1 == r_gpc_w1 & r_cpc_w1!=. | r_cpc_w1 == r_oth_w1 & r_cpc_w1!=. | r_cpc_w1 == r_amb_w1 & r_cpc_w1!=.
replace sum_tie_w1 = 999 if r_ndp_w1 == r_cpc_w1 & r_ndp_w1!=. | r_ndp_w1 == r_lpc_w1 & r_ndp_w1!=. | r_ndp_w1 == r_bq_w1 & r_ndp_w1!=. | r_ndp_w1 == r_gpc_w1 & r_ndp_w1!=. | r_ndp_w1 == r_oth_w1 & r_ndp_w1!=. | r_ndp_w1 == r_amb_w1 & r_ndp_w1!=.
replace sum_tie_w1 = 999 if r_lpc_w1 == r_cpc_w1 & r_lpc_w1!=. | r_lpc_w1 == r_ndp_w1 & r_lpc_w1!=. | r_lpc_w1 == r_bq_w1 & r_lpc_w1!=. | r_lpc_w1 == r_gpc_w1 & r_lpc_w1!=. | r_lpc_w1 == r_oth_w1 & r_lpc_w1!=. | r_lpc_w1 == r_amb_w1 & r_lpc_w1!=.
replace sum_tie_w1 = 999 if r_bq_w1 == r_cpc_w1 & r_bq_w1!=. | r_bq_w1 == r_ndp_w1 & r_bq_w1!=. | r_bq_w1 == r_lpc_w1 & r_bq_w1!=. | r_bq_w1 == r_gpc_w1 & r_bq_w1!=. | r_bq_w1 == r_oth_w1 & r_bq_w1!=. | r_bq_w1 == r_amb_w1 & r_bq_w1!=.
replace sum_tie_w1 = 999 if r_gpc_w1 == r_cpc_w1 & r_gpc_w1!=. | r_gpc_w1 == r_ndp_w1 & r_gpc_w1!=. | r_gpc_w1 == r_lpc_w1 & r_gpc_w1!=. | r_gpc_w1 == r_bq_w1 & r_gpc_w1!=. | r_gpc_w1 == r_oth_w1 & r_gpc_w1!=. | r_gpc_w1 == r_amb_w1 & r_gpc_w1!=.
replace sum_tie_w1 = 999 if r_oth_w1 == r_cpc_w1 & r_oth_w1!=. | r_oth_w1 == r_ndp_w1 & r_oth_w1!=. | r_oth_w1 == r_lpc_w1 & r_oth_w1!=. | r_oth_w1 == r_bq_w1 & r_oth_w1!=. | r_oth_w1 == r_gpc_w1 & r_oth_w1!=. | r_oth_w1 == r_amb_w1 & r_oth_w1!=.
replace sum_tie_w1 = 999 if r_amb_w1 == r_cpc_w1 & r_amb_w1!=. | r_amb_w1 == r_ndp_w1 & r_amb_w1!=. | r_amb_w1 == r_lpc_w1 & r_amb_w1!=. | r_amb_w1 == r_bq_w1 & r_amb_w1!=. | r_amb_w1 == r_gpc_w1 & r_amb_w1!=. | r_amb_w1 == r_oth_w1 & r_amb_w1!=.

* Generate value of 0 for lower sums of expectations

replace r_cpc_w1 = 0 if r_cpc_w1!=1
replace r_ndp_w1 = 0 if r_ndp_w1!=1
replace r_lpc_w1 = 0 if r_lpc_w1!=1
replace r_bq_w1 = 0 if r_bq_w1!=1
replace r_gpc_w1 = 0 if r_gpc_w1!=1
replace r_oth_w1 = 0 if r_oth_w1!=1
replace r_amb_w1 = 0 if r_amb_w1!=1

* Identify party with highest sums of expectations in each district (1 = CPC, 2 = LPC, 3 = NDP, 4 = BQ, 5 = GPC, 88 = other, 99 = ambiguous, 999 = tie)

gen r_w1 = .

replace r_w1 = 1 if r_cpc_w1 == 1
replace r_w1 = 3 if r_ndp_w1 == 1
replace r_w1 = 2 if r_lpc_w1 == 1
replace r_w1 = 4 if r_bq_w1 == 1
replace r_w1 = 5 if r_gpc_w1 == 1
replace r_w1 = 88 if r_oth_w1 == 1
replace r_w1 = 99 if r_amb_w1 == 1
replace r_w1 = 999 if sum_tie_w1 == 999


**********
** SAVE **
**********

save "ca2019_ces_web.dta", replace

* Drop district duplicates

drop if ability_w == .

duplicates tag constituencynumber, gen(dupCA19)
duplicates drop constituencynumber, force

drop if constituencynumber == .

* Generate number of respondents by district

gen numberCA19 = dupCA19 + 1

* Identify party with highest number of highest expectations for each district (1 = CPC, 2 = LPC, 3 = NDP, 4 = BQ, 5 = GPC, 88 = other, 99 = ambiguous)

gen p = .

replace p = 1 if mean_cpc > mean_ndp & mean_cpc > mean_lpc & mean_cpc > mean_bq & mean_cpc > mean_gpc & mean_cpc > mean_oth & mean_cpc > mean_amb // A plurality of respondents in the district forecast a victory of the CPC candidate
replace p = 3 if mean_ndp > mean_cpc & mean_ndp > mean_lpc & mean_ndp > mean_bq & mean_ndp > mean_gpc & mean_ndp > mean_oth & mean_ndp > mean_amb // A plurality of respondents in the district forecast a victory of the NDP candidate
replace p = 2 if mean_lpc > mean_cpc & mean_lpc > mean_ndp & mean_lpc > mean_bq & mean_lpc > mean_gpc & mean_lpc > mean_oth & mean_lpc > mean_amb // A plurality of respondents in the district forecast a victory of the LPC candidate
replace p = 4 if mean_bq > mean_cpc & mean_bq > mean_ndp & mean_bq > mean_lpc & mean_bq > mean_gpc & mean_bq > mean_oth & mean_bq > mean_amb // A plurality of respondents in the district forecast a victory of the BQ candidate
replace p = 5 if mean_gpc > mean_cpc & mean_gpc > mean_ndp & mean_gpc > mean_lpc & mean_gpc> mean_bq & mean_gpc > mean_oth & mean_gpc > mean_amb // A plurality of respondents in the district forecast a victory of the GPC candidate
replace p = 88 if mean_oth > mean_cpc & mean_oth > mean_ndp & mean_oth > mean_lpc & mean_oth > mean_bq & mean_oth > mean_gpc & mean_oth > mean_amb // A plurality of respondents in the district forecast a victory of another candidate
replace p = 99 if mean_amb > mean_cpc & mean_amb > mean_ndp & mean_amb > mean_lpc & mean_amb > mean_bq & mean_amb > mean_gpc & mean_amb > mean_oth // A plurality of respondents in the district made ambiguous forecasts
replace p = 999 if p == . // There is a tie between two or more candidates in the forecasts of respondents

* Identify correct and ambiguous/tied plurality forecasts at the district level (unweigthed)

gen p_correct = 0 if p!=district_outcome & p!=99 & p!=999
replace p_correct = 1 if p == district_outcome & p!=99 & p!=999
replace p_correct = 99 if p_correct == .

gen p_correct_d = p_correct
replace p_correct_d = 0 if p_correct_d == 99 // Treat ambiguous/tied plurality forecasts as incorrect

* Identify correct plurality-range forecasts at the district level

gen r_correct_w1 = 0 if r_w1!=district_outcome & r_w1!=999
replace r_correct_w1 = 1 if r_w1 == district_outcome & r_w1!=999
replace r_correct_w1 = 99 if r_w1 == 999

gen r_correct_w1_d = r_correct_w1
replace r_correct_w1_d = 0 if r_correct_w1_d == 99 // Treat tied range forecasts as incorrect

log close